简体   繁体   English

为什么在自定义PHP代码中出现SQL语法错误?

[英]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; 您的SQL语法有误; 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 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'='Damien', last_name ='Surname',其中id = '49'在第2行附近使用

My PHP is as follows: 我的PHP如下:

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 和HTML如下

<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... 如果我没记错,则需要SET关键字,并且要删除多余的逗号。如果我记错了,请纠正我...

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. 在下面的语句之后,您不需要使用comma

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

It should be as stated below. 应该如下所述。 Note that the comma has been removed from the end of this line. 请注意, comma已从该行的末尾删除。

`last_name`='$get[last_name]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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