写入 MySQL 数据库时如何处理撇号
我收到此错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 's','portal','','offering','MSNBC','News','','sports','', 附近使用的正确语法。第 3 行的“MSN”、“金钱”、“”、“游戏”
唯一的问题是在插入包含撇号的数据时会出现此错误。我尝试将数据类型从 VARCHAR
更改为 TEXT
,但结果仍然相同。
我尝试放入 addslashes()
我该如何解决这个问题?
$query=" INSERT INTO alltags
(id,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9,tag10,tag11,tag12,tag13,tag14,tag15,tag16,tag17,tag18,tag19,tag20,tag21,tag22,tag23,tag24,tag25,tag26,tag27,tag28,tag29,tag30)
VALUES
('',mysql_real_escape_string($uniqkey[0]),mysql_real_escape_string($uniqkey[1]),mysql_real_escape_string($uniqkey[2]),mysql_real_escape_string($uniqkey[3]),mysql_real_escape_string($uniqkey[4]),mysql_real_escape_string($uniqkey[5]),mysql_real_escape_string($uniqkey[6]),mysql_real_escape_string($uniqkey[7]),mysql_real_escape_string($uniqkey[8]),mysql_real_escape_string($uniqkey[9]),mysql_real_escape_string($uniqkey[10]),mysql_real_escape_string($uniqkey[11]),mysql_real_escape_string($uniqkey[12]),mysql_real_escape_string($uniqkey[13]),mysql_real_escape_string($uniqkey[14]),mysql_real_escape_string($uniqkey[15]),mysql_real_escape_string($uniqkey[16]),mysql_real_escape_string($uniqkey[17]),mysql_real_escape_string($uniqkey[18]),mysql_real_escape_string($uniqkey[19]),mysql_real_escape_string($uniqkey[20]),mysql_real_escape_string($uniqkey[21]),mysql_real_escape_string($uniqkey[22]),mysql_real_escape_string($uniqkey[23]),mysql_real_escape_string($uniqkey[24]),mysql_real_escape_string($uniqkey[25]),mysql_real_escape_string($uniqkey[26]),mysql_real_escape_string($uniqkey[27]),mysql_real_escape_string($uniqkey[28]),mysql_real_escape_string($uniqkey[29])) ";
mysql_query($query) or die(mysql_error());
我将其更改为mysql_real_escape_string
。这个语法正确吗?我收到错误。
I am getting this 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','portal','','offering','MSNBC','News','','sports','','MSN','Money','','games'' at line 3
The only problem is that this error shows up when inserting data that contains an apostrophe. I tried changing the data type from VARCHAR
to TEXT
, but the result is still the same.
I tried to put in addslashes()
How do I fix this?
$query=" INSERT INTO alltags
(id,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9,tag10,tag11,tag12,tag13,tag14,tag15,tag16,tag17,tag18,tag19,tag20,tag21,tag22,tag23,tag24,tag25,tag26,tag27,tag28,tag29,tag30)
VALUES
('',mysql_real_escape_string($uniqkey[0]),mysql_real_escape_string($uniqkey[1]),mysql_real_escape_string($uniqkey[2]),mysql_real_escape_string($uniqkey[3]),mysql_real_escape_string($uniqkey[4]),mysql_real_escape_string($uniqkey[5]),mysql_real_escape_string($uniqkey[6]),mysql_real_escape_string($uniqkey[7]),mysql_real_escape_string($uniqkey[8]),mysql_real_escape_string($uniqkey[9]),mysql_real_escape_string($uniqkey[10]),mysql_real_escape_string($uniqkey[11]),mysql_real_escape_string($uniqkey[12]),mysql_real_escape_string($uniqkey[13]),mysql_real_escape_string($uniqkey[14]),mysql_real_escape_string($uniqkey[15]),mysql_real_escape_string($uniqkey[16]),mysql_real_escape_string($uniqkey[17]),mysql_real_escape_string($uniqkey[18]),mysql_real_escape_string($uniqkey[19]),mysql_real_escape_string($uniqkey[20]),mysql_real_escape_string($uniqkey[21]),mysql_real_escape_string($uniqkey[22]),mysql_real_escape_string($uniqkey[23]),mysql_real_escape_string($uniqkey[24]),mysql_real_escape_string($uniqkey[25]),mysql_real_escape_string($uniqkey[26]),mysql_real_escape_string($uniqkey[27]),mysql_real_escape_string($uniqkey[28]),mysql_real_escape_string($uniqkey[29])) ";
mysql_query($query) or die(mysql_error());
I changed it to mysql_real_escape_string
. Is this syntax correct? I am getting errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对包含 MySQL 可能解释的字符的数据进行编码的过程称为“转义”。您必须使用 mysql_real_escape_string 转义字符串,这是一个 PHP 函数,而不是 MySQL 函数,这意味着您必须在 PHP 中运行它,然后才能将查询传递到数据库。您必须转义从外部源进入程序的任何数据。任何未转义的数据都可能是SQL 注入。
在构建查询之前,您必须转义数据。此外,您还可以使用 PHP 的循环结构和
range
以编程方式构建查询:The process of encoding data which contains characters MySQL might interpret is called "escaping". You must escape your strings with
mysql_real_escape_string
, which is a PHP function, not a MySQL function, meaning you have to run it in PHP before you pass your query to the database. You must escape any data that comes into your program from an external source. Any data that isn't escaped is a potential SQL injection.You have to escape your data before you build your query. Also, you can build your query programmatically using PHP's looping constructs and
range
:使用 mysql_real_escape_string 是处理 SQL 字符的更安全方法插入/更新:
另外,我会将您的列从 TEXT 更改回 VARCHAR - 除了索引之外,搜索效果更好。
更新更新
由于
id
是一个 auto_increment 列,您可以:将其保留在列列表之外,这样您就不必在 VALUES 子句中提供值:
在列列表中包含
id
,这要求您在 VALUES 子句中使用任一值:NULL
默认
这里是使用 NULL 作为 id 占位符的示例:
我想真的强调您不应该那样设置列。
Using mysql_real_escape_string is a safer approach to handling characters for SQL insertion/updating:
Also, I'd change your columns back from TEXT to VARCHAR - searching, besides indexing, works much better.
Update for your update
Being that
id
is an auto_increment column you can:leave it out of the list of columns, so you don't have to provide a value in the VALUES clause:
include
id
in the list of columns, which requires you use either value in its place in the VALUES clause:NULL
DEFAULT
Here's an example using NULL as the id placeholder:
I want to really stress that you should not setup your columns like that.
meagar 的答案略有改进:
编辑: meagar 更新了他的帖子,所以他的答案现在更好了。
Slight improvement of meagar's answer:
EDIT: meagar updated his post, so his answer is now better.
请看meagas的回答。这是正确的代码。
如果你想使用被误导的 mysql_query() 函数,那么你必须按如下方式分解 SQL 字符串:
或者因为你有一个数组,所以使用巧妙的方法调用来一次转义所有内容:
Please look at meagars answer. This is the correct code.
If you want to use the misguided mysql_query() function, then you have to break up the SQL string as follows:
Or since you have an array, use the clever method call to escape all at once: