Jump to content
Larry Ullman's Book Forums

Chapter 8: Registration Script Error


Recommended Posts

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:

 

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\Users\Mike\Webwork\XAMPP\xampp\htdocs\tests\register.php on line 91

 

 

Query: INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('Mike', 'Ford', 'Testaddy', SHA1('Test'), NOW() )

 

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\Users\Mike\Webwork\XAMPP\xampp\htdocs\tests\register.php on line 95

 

And when using the more involved error handling script covered in the debugging chapter the first line returned is:

An error occured in script 'C:\Users\Mike\Webwork\XAMPP\xampp\htdocs\tests\register.php' on line 98: Undefined variable: dbc

Array

 

 

MySQL Server version: 5.5.11

PhP version: 5.3.1 (I believe anyway)

Link to comment
Share on other sites

Try this, put thisn in your mysqli_connect.php file and see if it returns true:

<?

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() );

if ($dbc) {
   echo 'Connected';
} else {
   echo 'Not connected';
}
?>

This will confirm if $dbc exists in this script when you load mysqli_connect.php in your browser. Then load your register file and see if whether 'connected' or 'not connected' is echoed out.

  • Upvote 1
Link to comment
Share on other sites

Hi Jonathon thanks for the assist,

 

I tried what you said and noticed I wasn't actually getting anything at all back from either of the echo statements!

 

I cleared out all the script from mysqli_connect.php leaving just one echo statement and still got nothing from it.

 

So I made a new PHP file and copy and pasted the script (Just the PHP segments) over just to see what would happen and everything now works as it should.... really not sure what was going on there, seems very odd. :huh:

 

Happy that the problem is resolved, but now I can't figure out why the original script wasn't working to start with.

Link to comment
Share on other sites

Possibly because you've used

<?

and not

<?php

tags.

 

..... :unsure:

 

*cough*

 

Not sure how I missed that. Now I feel daft, but certainly less likely to happen again in the future!

 

Thanks again.

Link to comment
Share on other sites

 Share

×
×
  • Create New...