I am working through the PHP Fast & Easy book and have run into an error in the code, but I can't figure out what is not right.
When the code executes I get the following error:
You have an error in your SQL syntax near 'WHERE id = '" at line 13
I am not sure why the SQL query is erroring since it looks like a good structure to me. I have thought of the possibility that it may be one of the preceding pages that is not passing the appropriate info, but I don't think so since I have tried changing the line to end with WHERE id = 2"; and I get the same error (replacing the 2 in the error of course!) If anyone has an idea I would be open to figuring out why it doesn't work. I have downloaded the code for the book and get the same error, so it appears it is an actual problem in the book itself.
Thanks in advance.
Greg
Here is my code:
<?
if ((!$_POST[f_name]) || (!$_POST[l_name])) {
header( "Location: exit;
} else {
session_start();
}
if ($_SESSION[valid] != "yes"
{
header("Location: exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = @mysql_connect("localhost", "ghicks", "jester"
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "UPDATE $table_name SET
f_name = '$_POST[f_name]',
l_name = '$_POST[l_names]',
address1 = '$_POST[address1]',
address2 = '$_POST[address2]',
address3 = '$_POST[address3]',
postcode = '$_POST[postcode]',
country = '$_POST[country]',
prim_tel = '$_POST[prim_tel]',
sec_tel = '$_POST[sec_tel]',
email = '$_POST',
birthday = '$_POST[birthday]',
WHERE id = '$_POST[id]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="IBM WebSphere Studio Homepage Builder V6.0.0 for Windows">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>My Contact Management System: Contact Updated</title>
</head>
<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact - Contact Updated</em></h2>
<p>The following information was successfully updated in <? echo "$table_name"; ?></p>
<table cellspacing=3 cellpadding=5>
<tr>
<th>NAME AND ADDRESS INFORMATION</th>
<th>OTHER CONTACT/PERSONAL INFORMATION</th>
</tr>
<tr>
<td valign=top>
<p><strong>First Name:</strong><br>
<? echo "$_POST[f_name]"; ?></p>
<p><strong>Last Name:</strong><br>
<? echo "$_POST[l_name]"; ?></p>
<p><strong>Address 1:</strong><br>
<? echo "$_POST[address1]"; ?></p>
<p><strong>Address 2:</strong><br>
<? echo "$_POST[address2]"; ?></p>
<p><strong>Address 3:</strong><br>
<? echo "$_POST[address3]"; ?></p>
<p><strong>Zip/Postal Code:</strong><br>
<? echo "$_POST[postcode]"; ?></p>
<p><strong>Country:</strong><br>
<? echo "$_POST[country]"; ?></p>
</td>
<td valign=top>
<p><strong>Primary Telephone Number:</strong><br>
<? echo "$_POST[prim_tel]"; ?></p>
<p><strong>Secondary Telephone Number:</strong><br>
<? echo "$_POST[sec_tel]"; ?></p>
<p><strong>E-Mail Address:</strong><br>
<? echo "$_POST[email]"; ?></p>
<p><strong>Birthday:</strong><br>
<? echo "$_POST[birthday]"; ?></p>
</td>
</tr>
<tr>
<td align=center colspan=2><br>
<p><a href="contact_menu.php">Return to Main Menu</a></p>
</td>
</tr>
</table>
</body>
</html>
Greg Hicks
VB.Net (Newbie) Programmer
[URL unfurl="true"]http://www.ajlb2.com[/URL]
When the code executes I get the following error:
You have an error in your SQL syntax near 'WHERE id = '" at line 13
I am not sure why the SQL query is erroring since it looks like a good structure to me. I have thought of the possibility that it may be one of the preceding pages that is not passing the appropriate info, but I don't think so since I have tried changing the line to end with WHERE id = 2"; and I get the same error (replacing the 2 in the error of course!) If anyone has an idea I would be open to figuring out why it doesn't work. I have downloaded the code for the book and get the same error, so it appears it is an actual problem in the book itself.
Thanks in advance.
Greg
Here is my code:
<?
if ((!$_POST[f_name]) || (!$_POST[l_name])) {
header( "Location: exit;
} else {
session_start();
}
if ($_SESSION[valid] != "yes"
header("Location: exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = @mysql_connect("localhost", "ghicks", "jester"
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "UPDATE $table_name SET
f_name = '$_POST[f_name]',
l_name = '$_POST[l_names]',
address1 = '$_POST[address1]',
address2 = '$_POST[address2]',
address3 = '$_POST[address3]',
postcode = '$_POST[postcode]',
country = '$_POST[country]',
prim_tel = '$_POST[prim_tel]',
sec_tel = '$_POST[sec_tel]',
email = '$_POST',
birthday = '$_POST[birthday]',
WHERE id = '$_POST[id]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="IBM WebSphere Studio Homepage Builder V6.0.0 for Windows">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>My Contact Management System: Contact Updated</title>
</head>
<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact - Contact Updated</em></h2>
<p>The following information was successfully updated in <? echo "$table_name"; ?></p>
<table cellspacing=3 cellpadding=5>
<tr>
<th>NAME AND ADDRESS INFORMATION</th>
<th>OTHER CONTACT/PERSONAL INFORMATION</th>
</tr>
<tr>
<td valign=top>
<p><strong>First Name:</strong><br>
<? echo "$_POST[f_name]"; ?></p>
<p><strong>Last Name:</strong><br>
<? echo "$_POST[l_name]"; ?></p>
<p><strong>Address 1:</strong><br>
<? echo "$_POST[address1]"; ?></p>
<p><strong>Address 2:</strong><br>
<? echo "$_POST[address2]"; ?></p>
<p><strong>Address 3:</strong><br>
<? echo "$_POST[address3]"; ?></p>
<p><strong>Zip/Postal Code:</strong><br>
<? echo "$_POST[postcode]"; ?></p>
<p><strong>Country:</strong><br>
<? echo "$_POST[country]"; ?></p>
</td>
<td valign=top>
<p><strong>Primary Telephone Number:</strong><br>
<? echo "$_POST[prim_tel]"; ?></p>
<p><strong>Secondary Telephone Number:</strong><br>
<? echo "$_POST[sec_tel]"; ?></p>
<p><strong>E-Mail Address:</strong><br>
<? echo "$_POST[email]"; ?></p>
<p><strong>Birthday:</strong><br>
<? echo "$_POST[birthday]"; ?></p>
</td>
</tr>
<tr>
<td align=center colspan=2><br>
<p><a href="contact_menu.php">Return to Main Menu</a></p>
</td>
</tr>
</table>
</body>
</html>
Greg Hicks
VB.Net (Newbie) Programmer
[URL unfurl="true"]http://www.ajlb2.com[/URL]