What is the error in my sql sytanx

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

What is the error in my sql sytanx

Post by mohson »

When I echo my results at the end of each row I have a hyperlinked edit colum. when you click on the hyperlinked 'edit' it loads the record in a seperate form where you can change it and save the changes. Now this worked perfectly fine for all my tables but when I incorporated this update for a new table I created it doesnt work and I get the following Error
could not insertYou have an error in your SQL syntax near '1'' at line 1
Heres the code

Code: Select all

if(isset($_GET['person_id'])){
			$person_id=$_GET['person_id'];
		}

		if ($_POST['Submit'] == 'Save'){

			foreach ($_POST as $formName => $phpName){
				$$formName = $phpName;
			}
			
					 $sql ="UPDATE feedbackcontacts SET "
					. " salutation='"	.$salutation."'," 
					. " name='"	.$name."'," 
					. " surname='"	.$surname."'," 
					. " organisation='"	.$organisation."'," 
					. " email='"		.$email."'," 
					. " address='" .$address	."'," 
					. " address1='" .$address1	."'," 
					. " telephonefax='"	 .$telephonefax."'," 
					. " mobile='"	.	$mobile."'," 
					. " mscints='" .$mscints."'," 
					. " mscactive='" .$mscactive."'," 
					. " otheractivities='" .$otheractivities."'," 
					. " gradjobs='" .$gradjobs."'," 
					. " ugproj='" .	$ugproj	."'," 
					. " pdev='" .$pdev."'," 
					. " bcsmem='" .$bcsmem."',"
					. " bcspds='" .$bcspds."',"
					. " teach='" .$teach."',"
					. " acconsult='" .$acconsult.","
					. " WHERE person_id='" . $person_id."'";
I then echo the form like so:

Code: Select all

echo "<br><div align=center><br>";
			mysql_query($sql) or die('could not insert' . mysql_error());

			echo  "		<div align=left><table width=70% border=1 cellpadding=10 align=center>"
			. "		<tr>"
				. "			<td width=40%>"
				. "					<font face=arial size=2><b>"
				. "					Salutation"
				. "					</font>"
				. "				</td>"
				. "				<td>"
				. "					<font face=arial size=2>"
				. "					" . $salutation . "</font>"
				. "				</td>"
				. "			</tr>"

This continues for all the other table fields....................
Finally the the code finishes like this:

Code: Select all

$sql = "SELECT * FROM feedbackcontacts WHERE person_id='" . $person_id . "'";
Can anyone see what is wrong with this and why I get that error message
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$sql ="UPDATE `feedbackcontacts` SET "
. " `salutation`='" . $salutation . "',"
. " `name`='" . $name . "',"
. " `surname`='" . $surname . "',"
. " `organisation`='" . $organisation . "',"
. " `email`='" . $email . "',"
. " `address`='" . $address . "',"
. " `address1`='" . $address1 . "',"
. " `telephonefax`='" . $telephonefax . "',"
. " `mobile`='" . $mobile . "',"
. " `mscints`='" . $mscints . "',"
. " `mscactive`='" . $mscactive . "',"
. " `otheractivities`='" . $otheractivities . "',"
. " `gradjobs`='" . $gradjobs . "',"
. " `ugproj`='" . $ugproj . "',"
. " `pdev`='" . $pdev . "',"
. " `bcsmem`='" . $bcsmem . "',"
. " `bcspds`='" . $bcspds . "',"
. " `teach`='" .  $teach . "',"
. " `acconsult`='" . $acconsult . "' "
. " WHERE `person_id`='" . $person_id . "'";
There was a , after $acconsult even though there are no more fields after it in the query. It's also good practice to encapsulate field names in backticks. Try it now.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Thanks again for your patience and help it works perfectly well, really appreciate it, its the small things that bug me like this. Thanks again!!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Always a good idea to echo the query when you get issues like this to debug
Post Reply