php - MySQL SQL语法错误;查看与您的 MySQL 服务器版本相对应的手册,了解要使用的正确语法

标签 php mysql sql mysqli

<分区>

我正在尝试将示例博客文章插入到我在 MySQL 中的“帖子”表中(使用 PHP),但是每当提交大字 rune 章时我都会收到语法错误。如果我提交 20 个字符的内容,它可以工作,但 500 个字符之类的内容会引发以下错误:

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 ''uid', 'username', 'p_date', 'title', 'content') VALUES('1','Mark Twain', '2014-' at line 1

“内容”将通过 varchar(1000) 变量插入到数据库中。该表在 mysql 中定义为:

CREATE TABLE posts
(
    pid int NOT NULL AUTO_INCREMENT,
    uid int NOT NULL,
    username varchar(100) NOT NULL,
    p_date date NOT NULL,
    title varchar(225) NOT NULL,
    content varchar(10000) NOT NULL,
    PRIMARY KEY(pid),
    FOREIGN KEY(uid) REFERENCES users(uid)
);

我尝试提交的实际内容是这样的:

Secondly, these missionaries would gradually, and without creating suspicion or exciting alarm, introduce a rudimentary cleanliness among the nobility, and from them it would work down to the people, if the priests could be kept quiet. This would undermine the Church. I mean would be a step toward that. Next, education -- next, freedom -- and then she would begin to crumble. It being my conviction that any Established Church is an established crime, an established slave-pen, I had no scruples, but was willing to assail it in any way or with any weapon that promised to hurt it. Why, in my own former day -- in remote centuries not yet stirring in the womb of time -- there were old Englishmen who imagined that they had been born in a free country: a "free" country with the Corporation Act and the Test still in force in it -- timbers propped against men's liberties and dishonored consciences to shore up an Established Anachronism with.

插入语句如下:

$sql = "INSERT INTO posts ('uid', 'username', 'p_date', 'title', 'content') VALUES('$uid','$uname', '$date', '$title', '$content')";

if(!mysql_query($sql,$con)){
    echo "Oops! Something went wrong during the posting process. Please try again. ";
    die('Error: ' . mysql_error($con));
    header('Refresh: 1; URL=postingform.php');      
}else{
    // Now return the user to their post page 
    header('Refresh: 0; URL=postlist.php?uid='.$uid.'');
}

由于某种原因,它在 INSERT 过程中出错。我注意到的一件奇怪的事是日期在错误中被截断了。调用我正在使用的日期。 $date = date("Y-m-d");

我以前使用过相同的语法,没有出现任何问题。

****编辑

一些发贴者指出我的 INSERT 列语句中有单引号。我已将这些更改为支持抽动并完全删除它们,但错误仍然存​​在。

新错误:

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 's Court', 'Secondly, these missionaries would gradually, and without creating su' at line 1

我的插入语法仍然有问题,但我正在阅读的所有内容都表明它应该是正确的。

$sql = "INSERT INTO posts (`uid`, `username`, `p_date`, `title`, `content`) VALUES('$uid','$uname', '$p_date', '$title', '$content')";

最佳答案

删除(对于您的列)中的所有引号

('uid', 'username', 'p_date', 'title', 'content')

这些不是正确的列标识符

使用

(uid, username, p_date, title, content)

或使用反引号。

(`uid`, `username`, `p_date`, `title`, `content`)

但是作为快速引用,反引号主要用于保留关键字,或者如果表/列包含空格、连字符。


错误信息在这里让你知道

check the manual that corresponds to your MySQL server version for the right syntax to use near ''uid',
^--« right there

注意到 'uid' 之前的引号了吗?这就是问题开始的地方。


编辑:

使用准备好的语句尝试以下操作,并将 xxx 替换为您自己的凭据。

这应该可以解决您输入值中的引号问题。

您需要根据您的输入添加变量。

<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_USER = "xxx";
$DB_PASS = "xxx";

$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
  die('Connection failed [' . $conn->connect_error . ']');
}

$uid = ""; // replace with proper value
$uname = ""; // replace with proper value
$date = ""; // replace with proper value
$title = ""; // replace with proper value
$content = ""; // replace with proper value

$stmt = $conn->prepare("INSERT INTO posts (`uid`, `username`, `p_date`, `title`, `content`) VALUES (?, ?, ?, ?, ?)");

$stmt->bind_param('sssss', $uid, $uname, $date, $title, $content);

    if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }

        else{
          echo "Success";
        }

$stmt->close(); // Statement
$conn->close(); // MySQLi

脚注:

为了允许单引号和/或双引号,请基于以下内容,同时使用 stripslashes()功能。

$content = stripslashes($_POST['content']);

这将正确地输入数据库:
Bob 的姐姐今天来了,她说:“Bob,你的头发真可爱!”。

关于php - MySQL SQL语法错误;查看与您的 MySQL 服务器版本相对应的手册,了解要使用的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234777/

相关文章:

php - 优化 MySQL 查询,使用子查询; MariaDB 快得多

MySQL:选择上面的行

php - 数据库常量表与代码中的常量

mysql - 在查询中选择导致相反结果的查询

sql - 使用 Sqlite 将带有自动生成 id 字段的记录插入异步环境中的多个相关表中?

php - 插入图像到 MySQL 数据库

php - 在另一个查询中使用 MySQL 结果

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

sql - 在 Oracle SQL 中,如何从表中选择所有数据,但仅显示 DATE 数据类型列中特定于日期的行?

mysql - 强实体与弱实体 MYSQL