简体   繁体   English

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

[英]Error trying to insert key value into a MySQL table

I'm having problems trying to insert a key value (which I generate) into a table (jml_acymailing_subscriber). 我在尝试将一个键值(我生成的)插入表(​​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);

The key type is: 关键类型是:

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

And this is the error I get: 这是我得到的错误:

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

I have no problem inserting values like userid , name , created or any other ones. 插入useridnamecreated或其他任何值都没有问题。 Any one knows where is the problem? 谁知道问题出在哪里? I'm starting in PHP/SQL... 我是从PHP / SQL开始的......

Thank you! 谢谢! I really appreciate it! 我真的很感激!

key is reserverd word in mysql, so can use backticks key key是mysql中的reserverd word,所以可以使用反引号

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

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

Two things; 两件事情;

KEY is a reserved word in MySQL , so to use it as a field/table name it needs to be quoted with backticks (`) KEYMySQL中保留字 ,因此要将其用作字段/表名,需要使用反引号(`)引用

...and... ...和...

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

Corrected, that would result in; 更正,这将导致;

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

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

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

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

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