3

我正在尝试在 PHP 中创建一些基本的插入查询,它一直在给我一个错误,但我真的不知道为什么,你们可以看看有什么问题吗?

    mysql_query("
INSERT INTO itens (nome, data, cliente, link, desc, img) VALUES ($nome,$data,$cliente,$link,$desc,$img)
") or die(mysql_error());

更新

从 OP 的已删除答案中提取,代码现在是:

mysql_query("INSERT INTO itens (nome, data, cliente, link, `desc`, img) 
VALUES ($nome,$data,$cliente,$link,$desc,$img)") or die(mysql_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 'kl,j)' at line 2

kl 和 j 是我在表格中插入的最后两件事。

4

2 回答 2

7

DESC是 MySQL 保留关键字,您应该使用backtick, ex对其进行转义

INSERT INTO itens (nome, data, cliente, link, `desc`, img) 
VALUES ($nome,$data,$cliente,$link,$desc,$img)

您的查询很容易受到攻击SQL Injection,请花点时间阅读下面有关如何防止它的文章,

于 2012-12-18T15:59:23.197 回答
2

首先也是最重要的 - Escape、Escape、Escape学习 PDO / mysqli 和准备好的语句

第二 - 知道哪些保留关键字不能用于列名;这些必须使用反引号进行转义。

$sql = sprintf("INSERT INTO itens (nome, data, cliente, link, `desc`, img) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');", 
    mysql_real_escape_string($nome),
    mysql_real_escape_string($data),
    mysql_real_escape_string($cliente),
    mysql_real_escape_string($link),
    mysql_real_escape_string($desc),
    mysql_real_escape_string($img)
);

mysql_query($sql);

第三 - 我认为您在表名(itensvs items)中输入了错字。

于 2012-12-18T16:05:12.887 回答