I've been trying to pass data from one php file to another php file after the user login.
The goal is when a user registers to be redirected to another page where his statistics are displayed(username, money, ruby, diamond).
index.php
<?php include('server.php') ?>
<?php include('errors.php'); ?>
<div class="form-popupRegister" id="myRegForm">
<form method="post" action="server.php" class="form-containerReg">
<h1>Регистрирация</h1>
<label for="username"><b>Име</b></label>
<input type="text" name="username" placeholder="Въведете името на лейдито" value="<?php echo $username; ?>">
<label for="email"><b>Е-майл</b></label>
<input type="text" name="email" placeholder="Въведете e-mail" value="<?php echo $email; ?>">
<label for="password_1"><b>Парола</b></label>
<input type="password" placeholder="Въведете парола" name="password_1">
<label for="password_2"><b>Повторете Парола</b></label>
<input type="password" placeholder="Въведете парола повторно" name="password_2">
<button type="submit" class="btnReg" name="reg_user">Register</button>
<button type="button" class="btn-cancelReg" onclick="closeRegForm()">Close</button>
</form>
</div>
server.php
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$level = "";
$money = "";
$diamond = "";
$ruby = "";
$errors = array();
$ID = "";
$row = "";
// connect to the database
$db = mysqli_connect('localhost', 'id9159890_uregisterdb', 'testdb', 'id9159890_registerdb');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM register WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO register (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$queryTwo="SELECT ID FROM register WHERE username='$username'";
$results = mysqli_query($db, $queryTwo);
$resultsTwo = mysqli_fetch_assoc($results);
$ID = $resultsTwo['ID'];
$queryInsert="INSERT INTO mainuserdata (ID, money, diamond, rubin, level)
VALUES ('$ID', '0', '0', '0', '0')";
mysqli_query($db, $queryInsert);
$queryThree="SELECT * FROM mainuserdata WHERE ID='$ID'";
$resultsThree = mysqli_query($db, $queryThree);
while($row = mysqli_fetch_assoc($resultsThree)){
$username = $_SESSION['username'];
$level = $row['level'];
$money = $row['money'];
$diamond = $row['diamond'];
$ruby = $row['rubin'];
}
header('location: index2.php');
}else{
echo 'Unsuccessful registration!';
}
}
index2.php
<?php require('server.php') ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PwettyKittyPincesa</title>
<link href="./style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="navWrapper">
<div class="statistics">
<div class="profilePicture" name="profilePicture">
<label class="profilePictureLabel" for="profilePicture"><b><?php echo $username; ?></b></label>
</div>
<div class="money" name="money">
<output name="money" for="money"><?php echo $money; ?></output>
</div>
<div class="diamond" name="diamond">
<label class="diamondLabel" for="diamond"><b><?php echo $diamond; ?></b></label>
</div>
<div class="ruby" name="ruby">
<label class="rubyLabel" for="ruby"><b><?php echo $ruby; ?></b></label>
</div>
<div class="level" name="level">
<label class="levelLabel" for="level"><b>Level:<?php echo $level; ?></b></label>
</div>
</div>
</div>
</body>
</html>
Please, check out my code and help me out. I've been trying for so long that I feel that I'm about to lose my sanity. I think there is a bug in php.