尝试将键值插入MySQL表时出错

时间:2013-12-28 16:06:22

标签: php mysql sql

我在尝试将一个键值(我生成的)插入表(​​jml_acymailing_subscriber)时遇到问题。

$generateKey = md5(substr($email[1],0,strpos($email[1],'@')).rand(0,10000000));
$subid = 3603;
$sql2 = "UPDATE jml_acymailing_subscriber SET key='$generateKey', WHERE subid='$subid'";
$result2 = mysql_query($sql2,$con) or trigger_error(mysql_error(),E_USER_ERROR);

密钥类型是:

TYPE -->  varchar(250)  
ORDENATION --> utf8_general_ci
NULL --> yes
DEFAULT --> NULL

这是我得到的错误:

Fatal 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 
'key='15e3e092aa8672a6f7ad3e8a5a1db537', WHERE subid='3603'' at line 1 in 
/public_html/bootstrap3/donarAltaCatala.php on line 136

插入useridnamecreated或任何其他值时,我都没有问题。谁知道问题出在哪里?我从PHP / SQL开始......

谢谢!我真的很感激!

3 个答案:

答案 0 :(得分:2)

key是mysql中的保留字,因此可以使用反引号

$sql2 = "UPDATE jml_acymailing_subscriber SET `key`='$generateKey' WHERE subid='$subid'";

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

答案 1 :(得分:0)

两件事;

KEYreserved word in MySQL,因此要将其用作字段/表格名称,需要使用反引号(`)

进行引用

...和...

"UPDATE jml_acymailing_subscriber SET key='$generateKey', WHERE subid='$subid'"
                                                        ^ erroneous comma

更正,这将导致;

"UPDATE jml_acymailing_subscriber SET `key`='$generateKey' WHERE subid='$subid'"

答案 2 :(得分:0)

key='$generateKey'之后移除它,使其看起来像:

"UPDATE jml_acymailing_subscriber SET key='$generateKey' WHERE subid='$subid'";