0

我收到此错误错误: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 'to, from, message, read) VALUES ('3','2','testmessage','0')' at line 1.

我已经在这里待了几个小时,但在 SO 或任何其他站点都没有发现任何东西。提前感谢您的帮助。下面是一个简单的消息系统文件。

<?php
session_start();
$id = $_SESSION['id'];
$msg = $_POST['message'];
$theirs = $_POST['value'];
$read = 0;

$con = mysql_connect("127.0.0.1","root","");
mysql_select_db("test", $con);
if (!$con)
  die('Could not connect: ' . mysql_error());

$sql = "INSERT INTO  messages (to, from, message, read) VALUES ('$theirs','$id','$msg','$read')";

if (!mysql_query($sql,$con))
  die('Error: ' . mysql_error());

mysql_close($con);
4

2 回答 2

5

to,fromread是MySQL 中的保留字。用反引号引用它们

INSERT INTO  messages (`to`, `from`, `message`, `read`) VALUES ('$theirs','$id','$msg','$read')
于 2012-12-29T23:38:11.767 回答
1

from,toread是 MySQL 的保留字。最好不要将它们用作列名(否则必须将它们括在反引号中),而是使用其他列名。

代替from你可以使用sender,并使用recipient代替to。也read换成is_read,你很好。

于 2012-12-29T23:39:54.947 回答