Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am having a problem with the register.php page. When I fill out the form I am getting errors. When I created the mysqli_connect page it returned a blank page when I tested it. I think thats what it should do. I have the form in c://xampp and I am assuming that's where it should be. I'm sailing through the php sections but getting stuck on the db sections. The errors are:

 

System Error

 

You could not be registered due to a system error. We appologise for any inconveniance.

 

Notice: Undefined variable: dbc in C:\xampp\htdocs\scripts\register.php on line 65

 

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\scripts\register.php on line 65

 

 

 

Query: INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('Peter', 'Stubbs', 'peter@test.com', SHA1('grund1gps'), NOW())

 

Notice: Undefined variable: dbc in C:\xampp\htdocs\scripts\register.php on line 69

 

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\scripts\register.php on line 69

 

The register_php script is:

 

<?php # Script 8.3 - register.php

 

$page_title = 'register.php';

include ('includes/header.html');

// Check if the form has been submitted:

if (isset($_POST['submitted'])) {

$errors = array(); // Initialise an error array.

// Check for first name:

if (empty($_POST['first_name'])) {

$errors[] = 'You forgot to enter your first name.';

} else {

$fn = trim($_POST['first_name']);

}

// Check for last name:

if (empty($_POST['last_name'])) {

$errors[] = 'You forgot to enter your last name.';

} else {

$ln = trim($_POST['last_name']);

}

// Check for an email address:

if (empty($_POST['email'])) {

$errors[] = 'You forgot to enter your email address.';

} else {

$e = trim($_POST['email']);

}

// Check for a password and match against confirmed password:

if (!empty($_POST['pass1'])) {

if ($_POST['pass1'] != $_POST['pass2']) {

$errors[] = 'Your password did not match the confirmed password.';

} else {

$p = trim($_POST['pass1']);

}

} else {

$errors[] = 'You forgot to enter your password.';

}

if (empty($errors)) { //If everythings OK:

// Register the user in the database.

require_once ('C://xampp/mysqli_connect.php'); // Connect to the db.

 

// Make the query:

$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW())";

$r = @mysqli_query ($dbc, $q); // Run the query

if ($r) { // If it run OK.

// Print the message:

echo '<h1>Thank you!</h1>

<p>You are now registered. In chapter 11 you will actually be able to log in!</p><p><br /></p>';

} else { // If it did not run OK.

// Public Message

echo '<h1>System Error</h1>

<p class="error">You could not be registered due to a system error. We appologise for any inconveniance.</p>';

// Debug message:

echo '<p>' .mysqli_error($dbc) .'<br /><br />Query: ' .$q . '</p>';

} // End of IF.

mysqli_close($dbc); // Close the database.

// Include the footer and quit the script:

include ('includes/footer.html');

exit();

} else { // Report the errors:

echo '<h1>Error!</h1>

<p class="error">The following error(s) occured:<br />';

foreach ($errors as $msg) { // Print each error.

echo " - $msg<br b/>\n";

}

echo '</p><p>Please try again.</p><p><br /></p>';

} //End of if (empty($errors)) IF.

} // End of mail submit conditional.

?>

<h1>Register</h1>

<form action="register.php" method="POST">

<p>First Name: <input type="text" name="first_name" size="15" maxlengh="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<p>Last Name: <input type="text" name="last_name" size="15" maxlengh="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

<p>Email Address: <input type="text" name="email" size="20" maxlengh="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<p>Password: <input type="password" name="pass1" size="10" maxlengh="20" /></p>

<p>Confirm Password: <input type="password" name="pass2" size="10" maxlengh="20" /></p>

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

include ('includes/footer.html');

?>

 

and the mysqli_connect script is

 

</php # Script 8.2 - mysqli_connect.php

// This file contains the database access information.

// This file also establishes a connection to mysql

// and selects the database

// Set the database access information as Constants:

DEFINE ('DB_USER', 'root');

DEFINE ('DB_PASSWORD', 'grund1g');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'mydatabase');

// Make the connection

$dbc = @msqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to mysql: ' . mysqli_connect_error() );if ($dbc)

{

echo 'Connected';

} else {

echo 'Not connected';}

?>

 

Any help would be apreciated

Link to comment
Share on other sites

 Share

×
×
  • Create New...