Log in form problem - Please help!

Hi there, first things first;
I’d like to make clear that im just learning php, and hoping to get better at it as time goes on, and am willing to put in the effort required to do so!
Also, I am following a book, hence why the script is called 8.5 etc etc

Now, I’m not sure if there is a fault in my code - I copied it out of a book, so i dont know how it went wrong - and have checked and double checked that i copied it in right, and basically the problem is, is that the error messages i have created (which are to be displayed if wrong register information is provided) - appear as code when i execute the script.

The register form itself, comes out okay, aside from in ‘First Name’ and ‘Last Name’ php code is also displayed!

I will post my code, and if needs be, I can post a screen shot of what it looks like in a browser so to illustrate what im trying to explain. Any help would be appreiciated - as I really would like to progress further in my learning of php, and this has really left me stumped!

<?php # Script 8.3 - register.php $page_title= 'Register'; include('header.html'); //Check if the form has been submitted: if (isset($_POST['submitted'])) { $errors = array(); //Initialize an error array. //Check for a first name: if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = trim($_POST['first_name']); } //Check for a 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 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('../htdocs/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 ran OK. //Print a message: echo'

Thank you!

You are now registered. In chapter 11 you will actually be able to log in!


'; } else { //If it did not run OK. // Public message: echo '

System Error

You could not be registered due to a system error. We apologize for any inconvenience.

'; //Debugging message: echo '

' . mysqli_error($dbc) . '

Query: ' . $q . '

'; } // End of if ($r) IF. mysqli_close($dbc); //Close the database connection. //Include the footer and quit the script: exit(); } else { //Report the errors. echo '

Error!

The following error(s) occured:
'; foreach ($errors as $msg) { //Print each error. echo " - $msg
\n"; } echo '

Please try again.


'; } //End of if (empty($errors)) IF. } // End of the main submit conditional. ?>

Register

First name:

Last name:

Email Address:

Password:

Confirm Password:

when you wrote this bit
[sup]
//Check for a first name:
if (empty($_POST[‘first_name’])) {
$errors[] = ‘You forgot to enter your first name.’;
} else {
$fn = trim($_POST[‘first_name’]);
}
[/sup]

i suspect you copied and pasted it for the rest of the script because you forgot to change this bit:
[sup]$fn = trim($_POST[‘first_name’]);[/sup]

What you are doing is overwriting the variable each time!

here is how it should be
[sup]
//Check for a first name:
if (empty($_POST[‘first_name’])) {
$errors[] = ‘You forgot to enter your first name.’;
} else {
$fn = trim($_POST[‘first_name’]);
}
//Check for a 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’]);
}
[/sup]

;D

Yeh sorry!.. i did actually change that bit - but still, it has made no differences to the way the code executes :frowning:

Real sorry for wasting your time - i just realised what my noobish mistake was…

I was simply opening the file through my browser, rather than typing in localhost, followed by the file name

…my script executed fine after i realised this… oh god… i promise i wont do it again,…thanks for the help anyway :slight_smile:

glad you figured it out :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service