Jump to content
Larry Ullman's Book Forums

Recommended Posts

Thought I would be back but just not this soon. I am getting the following errror message:

 

Notice: Undefined variable: ln in C:\xampp\htdocs\scripts\register.php on line 50

 

Notice: Undefined variable: e in C:\xampp\htdocs\scripts\register.php on line 50

 

All the rest of the form works fine giving all the correct error messages when I brake the form on purpose but I keep getting these two lines of errors when I try and submit the completed form.

 

Could anyone help please I have includer the page code below.

 

<?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 {

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

}

// Check for an email address:

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

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

} else {

$fn = 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 = @msqli_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');

?>

 

Thanks inanticipation

 

Paul

Link to comment
Share on other sites

// Check for last name:
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$fn = trim($_POST['last_name']);
}
// Check for an email address:
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$fn = trim($_POST['email']);
}

 

the above 2 lines are causing your errors, as you keep setting the variable $fn, change them to

// 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']);
}

  • Upvote 1
Link to comment
Share on other sites

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

The line above should be

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

While you're testing your code you probably do not want to suppress errors - you would have picked this up immediately. I recommend following the tips in the chapter on debugging, it will save you hours of time and you'll find that you learn alot from finding your own errors.

  • Upvote 1
Link to comment
Share on other sites

Sorry to be a nusance, done that and I still get a blank page after I fill out the form.

 

Enclosed a copy of mysqli_connect page of that helps.

 

<?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', '*********');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', '************');

// Make the connection

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

?>

Link to comment
Share on other sites

The @ before all of your mysqli statements has the affect of suppressing the errors so you'll want to start by removing the @. also have you tried the steps outlined in the debugging chapter. I repeat this because I learned so much by doing this and found that I made far less errors and when I did make an error I was able to find the cause easily.

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...

Sorry its taken a while to get back. My computer broke and had to go in for repair.

 

I have taken all the @ s out and I now get the error:

 

Fatal error: Call to undefined function msqli_connect() in C:\xampp\mysqli_connect.php on line 14

 

I am told this is something to do with xampp itself and I have to change a file?

 

Have you come accross this before?

 

Thanks in anticipation

 

Paul

Link to comment
Share on other sites

 Share

×
×
  • Create New...