This php code is supposed to be used to update a table called contacts at a where the id= selected id. The variable $numresults checks that the id exists and sets the variable to a number, which should always be 1. Then the update statement updates the record where the id is. I get a syntax error 1064 when I run the code. When I run the code and change the ID after it has been selected it seems to work with no error. Where is my syntax going wrong?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<?php
$hi="hi";
echo $hi;
$servername = "localhost";
$username= "root";
$dbpassword = "";
$dbname="test";
$db=mysqli_connect($servername, $username, $dbpassword, $dbname);
if(!$db){
die("could not connect:".mysqli_connect_error());
} else{
$id=test_input($_POST['id']);
$firstname=test_input($_POST['firstname']);
$lastname=test_input($_POST['lastname']);
$pcat=test_input($_POST['pcat']);
$congroup=test_input($_POST['congroup']);
$cattype=test_input($_POST['cattype']);
$company=test_input($_POST['company']);
$position=test_input($_POST['position']);
$email=test_input($_POST['email']);
$website=test_input($_POST['website']);
$phone= test_input($_POST['phone']);
$mphone=test_input($_POST['mphone']);
$wphone=test_input($_POST['wphone']);
$fax=test_input($_POST['fax']);
$add1=test_input($_POST['add1']);
$add2=test_input($_POST['add2']);
$city=test_input($_POST['city']);
$state=test_input($_POST['state']);
$zip=test_input($_POST['zip']);
$country=test_input($_POST['country']);
$reference=test_input($_POST['reference']);
$entrydate=test_input($_POST['entrydate']);
$enteredby=test_input($_POST['enteredby']);
$notes=test_input($_POST['notes']);
$rtninfo = updateContact($db, $id, $firstname, $lastname, $pcat, $congroup ,$cattype, $company, $position,$email, $website, $phone, $mphone, $wphone, $fax, $add1, $add2, $city, $state, $zip, $country, $reference, $entrydate, $enteredby, $notes);
if ($rtninfo == "ContactNotFound")
{
print "<p style='color: red'>Contact Not Found - Check SSN</p>";
} else {
if ($rtninfo == "NotUpdated")
{
print "<p style='color: red'>Contact Not Updated</p>";
} else {
print "<p style='color: green'>Contact has been Changed";
}
}
}
?>
<?php
function updateContact($db, $id, $firstname, $lastname, $pcat, $congroup ,$cattype, $company, $position,$email, $website, $phone, $mphone, $wphone, $fax, $add1, $add2, $city, $state, $zip, $country, $reference, $entrydate, $enteredby, $notes)
{
//First check if SSN exists
$sql_statement = "SELECT id, firstname, lastname, pcat, congroup, cattype, company, position, email, website, phone, mphone, wphone, fax, add1, add2, city, state, zip, country, reference, entrydate, enteredby, notes ";
$sql_statement .= "FROM contacts ";
$sql_statement .= "WHERE id = '".$id."' ";
$result = mysqli_query($db, $sql_statement); // Run SELECT
$numresults = mysqli_num_rows($result);
// If SSN exists then Update the Contact Info
if ($numresults > 0)
{
$statement="UPDATE CONTACTS ";
"SET firstname='".$firstname."'";
/*"lastname='".$lastname."',";
"pcat='".$pcat."',";
"congroup='".$congroup."',";
"cattype='".$cattype."',";
"company='".$company."',";
"position='".$position."',";
"email='".$email."',";
"website='".$website."',";
"phone='".$phone."',";
"mphone='".$mphone."',";
"wphone='".$wphone."',";
"fax='".$fax."',";
"add1='".$add1."',";
"add2='".$add2."',";
"city='".$city."',";
"state='".$state."',";
"zip='".$zip."',";
"country='".$country."',";
"reference='".$reference."',";
"entrydate='".$entrydate."',";
"enteredby='".$enteredby."',";
"notes='".$notes."' ";*/
"WHERE id='".$id."' ";
$result = mysqli_query($db, $statement);
if ($result)
{
return $id;
} else {
$errno = mysqli_errno($db);
echo("<h4>MySQL No: ".mysqli_errno($db)."</h4>");
echo("<h4>MySQL Error: ".mysqli_error($db)."</h4>");
echo("<h4>SQL: ".$statement."</h4>");
echo("<h4>MySQL Affected Rows: ".mysqli_affected_rows($db)."</h4>");
return 'NotUpdated';
}
} else {
return 'ContactNotFound';
}
mysqli_close($db);
}
function test_input($data){
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
?>
</body>
</html>