Need beginner help with a code fragment

Hi,

The following code is from 'PHP Programming with MySQL", it had some errors which I thought I fixed, but it still won’t work right. It’s telling me the variables last_name and first_name are not defined on line 55. Here’s the code:

Guest Book Script

Enter your name to sign the guestbook

First Name

Last Name

<?php 
	if (empty($_POST['first_name']) || empty($_POST['last_name']))
		echo "<p>You must enter your first and last name! Click your browser's 'back' button to return to the Guest Book</p>";
		
	else {
		$DBConnect = @mysql_connect("localhost", "aatay", "cpt262");
		if ($DBConnect === FALSE)
			echo "<p>Unable to connect to database server.</p>"
			. "<p>Error code " . mysql_errno()
			. ": " . mysql_error() . "</p>";				
		else {
			$DBName = "guestbook";
			if (!@mysql_select_db($DBName, $DBConnect)) {
				$SQLstring = "CREATE DATABASE $DBName";
				$QueryResult = mysql_query($SQLstring, $DBConnect);
				if ($QueryResult === FALSE)
					echo "<p>Unable to execute the query.</p>"
						. "<p>Error code " . mysql_errno()
						. ": " . mysql_error($DBConnect) . "</p>";
				else
					echo "<p>You are the first visitor</p>";
			}
		mysql_select_db($DBName, $DBConnect);			
	
	$TableName = "visitors";
	$SQLstring = "SHOW TABLES LIKE '$TableName'";
	$QueryResult = @mysql_query($SQLstring, $DBConnect);
	if (mysql_num_rows($QueryResult) ==0) {
		$SQLstring = "CREATE TABLE $TableName (countID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, last_name VARCHAR(40), first_name VARCHAR(40))";
		$QueryResult = @mysql_query($SQLstring, $DBConnect);
		if ($QueryResult === FALSE)
			echo "<p>Unable to create the table.</p>"
					. "<p>Error code " . mysql_errno()
					. ": " . mysql_error($DBConnect) . "</p>";		
	
		$Last_Name = stripslashes($_POST['last_name']);
		$First_Name = stripslashes($_POST['first_name']);
		$SQLstring = "INSERT INTO $TableName VALUES (NULL, '$last_name', '$first_name')";
		if ($QueryResult === FALSE)
			echo "<p>Unable to execute the query.</p>"
				. "<p>Error code " . mysql_errno()
				. ": " . mysql_error($DBConnect) . "</p>";
		else
			echo "<h2>Thank you for signing the guestbook!</h2>";
		}
		mysql_close($DBConnect);
	}
}
?>

You use a capitol to start these variables:

[php]$Last_Name = stripslashes($_POST[‘last_name’]);
$First_Name = stripslashes($_POST[‘first_name’]);[/php]

You use lowercase in these variables:
[php]$SQLstring = “INSERT INTO $TableName VALUES (NULL, ‘$last_name’, ‘$first_name’)”;[/php]

Change $last_name & $first_name to $Last_name $First_name and you should be ok
:wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service