I double checked my code compared to the book and I'm sure I typed the code in correctly. The page loads, the errors display if I leave a field blank or if the password and confirm password fields don't match. However, if I complete the form correctly and submit, the MySQL database doesn't get the information passed to it. The "Registered!" and login page doesn't show up either. I checked the MySQL database through Ubuntu directly and the users table is still empty (Empty set (0.00 sec)). phpMyAdmin also returns empty, "MySQL returned an empty result set (i.e. zero rows). (Query took 0.0011 seconds)", for the users table as well.
<!DOCTYPE HTML>
<html lang="en">
<head>
<metacharset="UTF-8">
<title>Register Page</title>
</head>
<body>
<?php
$page_title = 'Register';
include('/var/www/html/learn/php/header.html');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
require('/var/www/connect_db.php');
$errors = array();
if(empty($_POST['first_name']))
{
$errors[] = 'Enter your first name.';
}
else
{
$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
}
if(empty($_POST['last_name']))
{
$errors[] = 'Enter your last name.';
}
else
{
$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
}
if(empty($_POST['email']))
{
$errors[] = 'Enter your email address.';
}
else
{
$e = mysqli_real_escape_string($dbc, trim($_POST['email']));
}
if(!empty($_POST['pass1']))
{
if($_POST['pass1'] != $_POST['pass2'])
{
$errors[] = 'Passwords do not match.';
}
else
{
$p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
}
}
else
{
$errors[] = 'Enter your password.';
}
if(empty($errors))
{
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysqli_query($dbc, $q);
if(mysqli_num_rows($r) != 0)
{
$errors[] = 'Email address already registered. <a href="login.php">Login</a>';
}
}
if(empty($errors))
{
$q = "INSERT INTO users
(first_name, last_name, email, pass, reg_date)
VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW())";
$r = mysqli_query($dbc, $q);
if($r)
{
echo '<h1>Registered!</h1>
<p>You are now registered.</p>
<p><a href="login.php">Login</a></p>';
}
mysqli_close($dbc);
include('/var/www/html/learn/php/footer.html');
exit();
}
else
{
echo '<h1>Error!</h1>
<p id="err_msg">The following error(s) occurred:<br>';
foreach($errors as $msg)
{
echo "- $msg<br>";
}
echo 'Please try again.</p>';
mysqli_close($dbc);
}
}
?>
<h1>Register</h1>
<form action="register.php" method="POST">
<p>
First Name: <input type="text" name="first_name" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name'];?>">
Last Name: <input type="text" name="last_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name'];?>">
</p>
<p>
Email Address: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>">
</p>
<p>
Password: <input type="password" name="pass1" value="<?php if(isset($_POST['pass1'])) echo $_POST['pass1'];?>">
Confirm Password: <input type="password" name="pass2" value="<?php if(isset($_POST['pass2'])) echo $_POST['pass2'];?>">
</p>
<p>
<input type="submit" value="Register">
</p>
</form>
<?php include('/var/www/html/learn/php/footer.html');?>
</body>
</html>
This is where I believe my problem is, but I've tried various alternatives and still cannot get the data to insert users into the MySQL database:
if(empty($errors))
{
$q = "INSERT INTO users
(first_name, last_name, email, pass, reg_date)
VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW())";
$r = mysqli_query($dbc, $q);
if($r)
{
echo '<h1>Registered!</h1>
<p>You are now registered.</p>
<p><a href="login.php">Login</a></p>';
}
mysqli_close($dbc);
include('/var/www/html/learn/php/footer.html');
exit();
}
else
{
echo '<h1>Error!</h1>
<p id="err_msg">The following error(s) occurred:<br>';
foreach($errors as $msg)
{
echo "- $msg<br>";
}
echo 'Please try again.</p>';
mysqli_close($dbc);
}
I'm not sure why the book is making me type "if($r)", but please tell me what it means and show where I messed up.