Jump to content

Only half of the code in Chapter two of HF PHP and MySQL works

Baal's Photo
Posted Aug 22 2011 02:47 PM

I have a website on Bluehost.com and selected Standard PHP. The code I downloaded from HeadFirstLabs.com for Report.php only runs the last PHP code. Here is the output and the code. What am I doing wrong?


The output in my browser:

Aliens Abducted Me - Report an Abduction

Thanks for submitting the form.
You were abducted and were gone for
Number of aliens:
Describe them:
The aliens did this:
Was Fang there?
Other comments:
Your email address is

The Code downloaded from Headfirstlabs.com:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aliens Abducted Me - Report an Abduction</title>
<h2>Aliens Abducted Me - Report an Abduction</h2>

$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];

$to = 'vithant@gmail.com';
$subject = 'Aliens Abducted Me - Abduction Report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
"Number of aliens: $how_many\n" .
"Alien description: $alien_description\n" .
"What they did: $what_they_did\n" .
"Fang spotted: $fang_spotted\n" .
"Other comments: $other";
mail($to, $subject, $msg, 'From:' . $email);

echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br />';
echo 'Number of aliens: ' . $how_many . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'The aliens did this: ' . $what_they_did . '<br />';
echo 'Was Fang there? ' . $fang_spotted . '<br />';
echo 'Other comments: ' . $other . '<br />';
echo 'Your email address is ' . $email;


1 Subscribe

2 Replies

 : Aug 23 2011 06:43 AM
Hi Baal,

There's a forum dedicated to Head First PHP & MySQL at http://forums.oreill...irst-php-mysql/. The forum is really active, so you may get an answer more quickly there.

Also, feel free to email booktech@oreilly.com, our book technical support group, if you don't get an answer on the forums.

Rachel James
O'Reilly Media
  dome123's Photo
Posted Oct 24 2011 02:18 PM

hello there, i read your post and i think i can give you the answer!!

there is actually no error in this code but just you are not putting nothing into the $_POST variables at this point so you should make a form on this page or another and use this page as action and all the informations will be processed then!!!

just check if something has been posted and then process the informations and try to make checks on the users input like

if($_POST > 0){

$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];


but you would check every single piece of data like this

if ($_POST > 0){
if ($_POST['firstname'] < 3){
$error_message="name too short";
else $firstname=$_POST['firstname'];
try to check as much as possible on user input

i hope this will help or i can write you a more detailed information, by the way i did the php sql certificate at o'reilly and you will have complete control over the language after you do that

have fun