1

我不敢相信我有这个问题。我一直在寻找和寻找,但我看不出有什么问题。我讨厌这个错误信息。

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 ' poster_ip, message, posted, thread_id INTO posts ' at line 1



mysql_query("INSERT poster, poster_ip, message, posted, thread_id
                INTO posts
                VALUES (
    {$post_info['poster']}, 
    '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', 
    '".mysql_real_escape_string($post_info['message'])."', 
    {$post_info['posted']}, 
    {$post_info['thread_id']}") or die (mysql_error());
4

4 回答 4

7

您的 SQL 语法错误

您应该使用类似于:

INSERT INTO posts (poster, poster_ip, message, posted, thread_id) VALUES (...)
于 2010-07-27T03:23:44.097 回答
3

也许你应该看一下文档;) 插入语法

如果要放置列名,则应将其放在表名之后。

示例: INSERT INTO table (col1, col2) VALUES (val1, val2)

于 2010-07-27T03:25:17.823 回答
1

看起来是练习一些调试技术的好机会。尝试构建您传递给函数的字符串并将其分配给一个变量,然后回显该变量以查看您实际传递给函数的内容。通过这种方式,您可以学到很多关于为什么会出错的信息。此外,了解您正在向其中插入值的列的数据类型也会有所帮助。

于 2010-07-27T03:27:35.053 回答
0

我编写这段代码是为了向您展示为什么数组对于查询生成很有用,并且如果您将来需要添加更多字段,则不太可能出现语法错误。

$fields = array('poster, poster_ip, message, posted, thread_id'); // Our fields
$table = 'posts'; // Our table name
$values = array(
    $post_info['poster'], 
    $_SERVER['REMOTE_ADDR'], 
    $post_info['message'], 
    $post_info['posted'], 
    $post_info['thread_id']
);
$values = array_map('mysql_real_escape_string', $values); // Secure all inputs
// Generate query
$query = "INSERT INTO $table (" . implode(',', $fields) . ") VALUES ('" . implode("','", $values . "')";
// Run query
$result = mysql_query($query) or die('query error: ' . mysql_error());
于 2010-07-27T11:35:52.017 回答