简体   繁体   中英

Why am I getting SQL syntax error in custom PHP code?

I'm trying to build an update query but I keep getting the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='Damien', last_name ='Surname', where id ='49'' at line 2

My PHP is as follows:

if($get['cmd'] == 'sname')
{
mysql_query("update users 
`first_name`='$get[first_name]', 
`last_name`='$get[last_name]',
where `id`='$get[id]'") or die(mysql_error());
//header("Location: $ret"); 
echo "changes done";
exit();
}

and HTML as follows

<input name="doSave" type="button" id="doSave" value="Save" onclick='$.get("dos.php",{ cmd: "sname",first_name:$("input#first_name").val(),last_name:$("input#last_name").val(),id: "<?php echo $row_settings['id']; ?>" } ,function(data){ $("#msg").html(data); });'>

Can anyone see what's wrong in my code that will be giving me this error?

If i am not wrong SET keyword is required and the extra comma is to be removed..correct me if I am wrong...

if($get['cmd'] == 'sname')
{
    mysql_query("update users SET 
    first_name ='$get[first_name]', 
    last_name ='$get[last_name]'
    where id ='$get[id]'") or die(mysql_error());
    //header("Location: $ret"); 
    echo "changes done";
    exit();
 } 

You have a comma after the below statement that is not required.

`last_name`='$get[last_name]',

It should be as stated below. Note that the comma has been removed from the end of this line.

`last_name`='$get[last_name]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM