I have been going through the book and haven't had any real problems up until I get to Chapter 8 and come to the user registration script.
The problem seems to be that '$dbc' is not being referenced from 'mysqli_connect.php' and so contains a NULL value when running the query, but I can't find the problem with any of the code itself so hopefully someone here will be able to spot where I am going wrong.
Register.php code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register script</title>
</head>
<body>
<?php
$page_title = 'Register';
include ('includes/header2.html');
if (isset($_POST['submitted'])) { // Check if the form has been submitted
$errors = array();
// Various validation checks on submitted form
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
}
else {
$fn = trim($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name';
}
else {
$ln = trim($_POST['last_name']);
}
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter a valid email';
}
else {
$e = trim($_POST['email']);
}
if (!empty($_POST['pass1'])) {
if ($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Your passwords do not match';
}
else {
$p = trim($_POST['pass1']);
}
}
else {
$error[] = 'You forgot to enter your password';
}
if (empty($errors)) { // If all validation checks find no problem
require_once('../mysqli_connect.php');
$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )"; // Assign SQL query to variable
$r = @mysqli_query ($dbc, $q); // Execute query
if ($r) { // If connection and execution return true
echo '<h1>Thank you!</h1>
<p>You are now registered. Once further functionality is added you will actually be able to log in!</p> <p><br /></p>';
}
else { // If connection or execution return false
echo '<h1>System Error</h1>
<p class="error"> You could not be reigistered due to a system error. We apoligise for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
}
mysqli_close($dbc);
include ('includes/footer.html');
exit();
} // End of form is valid execution
else { // If form validation checks find errors
echo '<h1>Error!</h1>
<p class="error"> The following error(s) occurred: <br />';
foreach ($errors as $msg) { // Print each error message stored in errors array
echo " - $msg<br />\n";
}
echo '</p> <p>Please try again.</p><p><br /></p>';
} // End of form error reporting
} // End of submit check
?>
<!-- Create registration form -->
<h1>Register</h1>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /> </p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /> </p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_post['email']; ?>" /> </p>
<p>Password: <input type="password" name="pass1" size="10" maxlength="20" /> </p>
<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /> </p>
<p> <input type="submit" name="submit" value="Register" /> </p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<!-- End of registration form creation -->
<?php
include('/includes/footer.html');
?>
</body>
</html>
And the mysqli_connect.php code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SQL Connection Script</title>
</head>
<body>
<?
DEFINE ('DB_USER', 'xxxxx');
DEFINE ('DB_PASSWORD', 'xxxxxx');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect MySQL: ' . mysqli_connect_error() );
?>
</body>
</html>
The mysqli_connect.php script appears to work fine when loaded itself and returns just a blank page.
When using var_dump($dbc); to check the value of dbc in register.php it returns a NULL value.
Tried moving mysqli_connect.php to the same directory as the script calling it but still get the same error.
The errors given by registration.php are:
And when using the more involved error handling script covered in the debugging chapter the first line returned is:
MySQL Server version: 5.5.11
PhP version: 5.3.1 (I believe anyway)