开发者

Why does my SQL string cause a "Error Message: Parse error: syntax error" in PHP?

This code keeps erroring.

Error Message: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/soulz/public_html/inbox.php on line 19

Here is the code:

mysql_query("UPDATE 开发者_运维知识库`messages` SET message_title = '[NO SUBJECT]' WHERE `message_id`=$row['message_id']");


Use curly braces:

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]' 
             WHERE `message_id`={$row['message_id']}");


Don't put apostrophes around the field name:

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]'
             WHERE `message_id`=$row[message_id]");
                                     ^^^^^^^^^^

Inside quoted strings, you cannot use additional quotation marks for array field names. There's an alternative, more elaborate syntax involving braces if you have a very complicated array expression, but you don't need that here.


It seems message_id is integer, so you can fix that error with a best practice.

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]'
             WHERE `message_id`=" . intval($row['message_id']));

You can use strval() for strings. Both functions are detailed in intval() manual page and strval() manual page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜