Jump to content
Larry Ullman's Book Forums

Ch8 Script 8.3 No Error Msg'S Received & No Data Added To Db


Recommended Posts

mac OSX snow leopard

apache 2.0.64

mysql 5.5.9

mysqli 5.5.9

MAMP

(hope that's the right info!)

 

I seem to have reached a sticking point at chapter 8. I have entered script 8.3 (and preceding db connection script etc) and when I run it, initially everything seems fine. The form comes up with the header and footer and css. I seem to be able to connect fine to the database, as I tested the 'mysqli_connect.php' script and a subsequent script:

 

<?php

$link = mysql_connect('localhost', 'root', 'root');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

?>

 

which indicated a successful connection.

 

The problems lie when:

1) I enter all the details correctly and click submit: the form resets recalling (sticky style) only first and last name. No info is entered into the database (on checking thru terminal) and no congratulations msg is displayed :o( (sad panda!)

2) I intentionally miss some of the registration fields to test the error reporting: the form resets recalling (sticky style) only first and last name. No user (or debugging) error msg is displayed :o( (sad panda!)

 

I tried omitting the @from the @mysqli_query but the same thing happens.

 

Just tested locally so far. Here's my script, which I have scrupulously checked but have probably missed something vital!!

 

 

 

<?php # register.php

 

$page_title = 'Register';

include ('includes/header.html');

 

// Check if form has been submitted:

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

 

$errors = array(); //Initialize 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']);

}

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 your email address.';

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

}

 

// Check for a password and match against the 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 everything's ok.

// Register the user in the database...

require_once('mysqli_connect.php'); // connect to the database

 

// Make the query:

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

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

if ($r) {// if it ran ok - Print message:

 

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

<p>You are now registered.</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. </p>';

 

// debugging message:

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

} //End of if ($r) IF.

 

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

 

// 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) occurred:<br />';

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

 

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

}

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

 

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

} // End of main Submit conditional

?>

 

<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="pass2" size="10" maxlength="20" /></p>

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

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

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

 

</form>

<?php

include ('includes/footer.html');

?>

 

 

I've been really enjoying the book so far - hope you can help me out!!

 

cheers,

 

Nick

 

p.s. I just tried the 'retrieving queries' script 8.4 and that works fine for me.

Link to comment
Share on other sites

Your using mysqli to execute the query but connecting using mysql. You need to use one or the other, you can't mix them. :)

Thanks Jonathon... sorry be being slow, but which part of the code do I need to change? I can only see mysqli's? I was just copying it out the book, but must have missed an 'i' or something!

Link to comment
Share on other sites

I seem to have reached a sticking point at chapter 8. I have entered script 8.3 (and preceding db connection script etc) and when I run it, initially everything seems fine. The form comes up with the header and footer and css. I seem to be able to connect fine to the database, as I tested the 'mysqli_connect.php' script and a subsequent script:

 <?php 
$link = mysql_connect('localhost', 'root', 'root'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully';
mysql_close($link); 
?>

 

you need to change that to

 

$link = mysqli_connect('localhost', 'username', 'password', 'database_name');

if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);

 

That should connect you to the database using mysqli then try the query

Link to comment
Share on other sites

Ah... I see! I just used that script independently, however, just to see if I was able to connect to the db - I'm not running that script in conjunction with the Register script.

 

My connect to db script is:

 

<title>mysqli_connect</title>

 

 

<?php # 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', 'root');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'sitename');

 

// Make the connection:

 

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

 

?>

 

As I say, connection to the database doesn't seem to be a problem, just getting that script to work!

Link to comment
Share on other sites

Remove the @ symbol from this $dbc and the mysqli_query($dbc, $q) line

 

also your SQL query you've put

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

 

have you spelt registration that way on purpose? Also, try running the query in the phpmyadmin console but replace the variables with dummy text and see if it executes. like:

 

INSERT INTO users (first_name, last_name, email, pass, reqistration_date) VALUES ('Elvis', 'Presley', 'elvis@isntdead.com', SHA1('password'), NOW())

 

If it doesn't execute what errors does it return?

Link to comment
Share on other sites

Many thanks for your help, Jonathon, I really appreciate it!

 

Well spotted on the q vs g typo! I missed that one!

 

I ran the query in terminal and it ran fine. Query ok 1 row affected.

 

I think the problem must be with the form part of the script, but I still can't see what!

Link to comment
Share on other sites

I still get the same problems happening though:

 

1) I enter all the details correctly and click submit: the form resets recalling (sticky style) only first and last name. No info is entered into the database (on checking thru terminal) and no congratulations msg is displayed

2) I intentionally miss some of the registration fields to test the error reporting: the form resets recalling (sticky style) only first and last name. No user (or debugging) error msg is displayed

Link to comment
Share on other sites

At this stage I would start echoing out values to see what's being passed to the form. I sadly can't replicate your entire set up with DB (Well I could) but unfortunately I'm fairly busy today. But check all your conditionals, even the if(isset($_POST['submitted']))

 

so for instance:

if (empty($_POST['first_name'])) {
     $errors[] = 'You forgot to enter your first name.';
} else { 
     $fn = trim($_POST['first_name']);
     echo $fn;
}

 

Go through like this trying to find where the script fails. I did read over your file and it looked ok, but I'm doing it here minus a text editor, but I would imagine you're using one, so it would be able to pick up any parse errors.

Link to comment
Share on other sites

Thanks again Jonathon, I did as you said - also checking the source code produced by the browser - as I did, I noticed an '=' missing from the email value field, which explained that.

 

Also on the 50th time of inspecting the script I noticed I'd put:

 

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

 

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

 

Works fine now!

 

Newb error!

 

Anyway, I really appreciate your timely and helpful responses - enjoy the rest of the day!!

Link to comment
Share on other sites

 Share

×
×
  • Create New...