php 单引号 数据库,关于php:如何在写入Mysql数据库时处理撇号’单引号

本问题已经有最佳答案,请猛点这里访问。

我得到这个错误:

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

唯一的问题是,插入包含撇号的数据时会出现此错误。我尝试将数据类型从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。这个语法正确吗?我出错了。

ID是一个自动增量,所以我把它留空了

使用PDO…它可以同时解决两个问题。PHPNET/PDO

包含MySQL可能解释的字符的数据编码过程称为"转义"。必须使用mysql_real_escape_string转义字符串,它是一个php函数,而不是mysql函数,这意味着在将查询传递到数据库之前,必须在php中运行它。必须从外部源中转义进入程序的任何数据。任何未转义的数据都可能是SQL注入。

在构建查询之前,必须先转义数据。此外,您还可以使用php的循环结构和range以编程方式构建查询:

// Build tag fields

$tags = 'tag' . implode(', tag', range(1,30));

// Escape each value in the uniqkey array

$values = array_map('mysql_real_escape_string', $uniqkey);

// implode values with quotes and commas

$values ="'" . implode("', '", $values) ."'";

$query ="INSERT INTO alltags (id, $tags) VALUES ('', $values)";

mysql_query($query) or die(mysql_error());

从概念上讲,这很有意义。我仍在尝试将您的代码合并到我的代码中。谢谢你的意见

@Karthik重要的是,在您将数据传递给mysql_query之前,您认识到mysql_real_escape_string是在PHP中运行的。我不知道OMG小马是怎么搞错的…

使用mysql_real_escape_string是处理SQL插入/更新字符的一种更安全的方法:

另外,我将把您的列从文本改回varchar-搜索,除了索引之外,工作得更好。更新您的更新

由于id是一个自动递增列,您可以:

下面是一个使用空值作为ID占位符的示例:

我想强调的是,您不应该这样设置列。

我编辑了我的问题。我的语法有什么问题吗?

@小马:这个密码真的是错的。我会调查谁给了你四张赞成票,也会杀了他们。不能将PHP函数调用混合到SQL字符串中。你必须跳出"字符串"并调用.mysql_real_escape()。每个变量单独使用。

你现在一定很晚了,你通常都比那好。

@小马,请把这个修好。在很多层面上都是错误的!

美加的回答略有改进:

编辑:美加更新了他的帖子,所以他的回答现在更好了。

$query = 'INSERT INTO alltags (id, ';

// append tag1, tag2, etc.

$query .= 'tag' . implode(', tag', range(1, 30)) .") VALUES ('',";

// escape each value in the uniqkey array

$escaped_tags = array_map('mysql_real_escape_string', $uniqkey);

// implode values with quotes and commas, and add closing bracket

$query .="'" . implode("', '", $escaped_tags) ."')";

// actually query

mysql_query($query) or die(mysql_error());

+1就像你构建标签串的方式一样;现在很晚了,我很累。

请看我的回答。这是正确的代码。

如果要使用错误引导的mysql_query()函数,则必须按如下方式分解SQL字符串:

或者,由于您有一个数组,请使用巧妙的方法调用立即全部转义:

$uniqkey = array_map("mysql_real_escape_string", $uniqkey);

mysql_query("USE THE ESCAPED ARRAY THEN DIRECTLY ('$uniqkey[0]', '$uniqkey[1]', '$uniqkey[2]', '$uniqkey[3]', ...");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值