我试图在数据库中插入段落(包含中间单引号),如下所示。插入段落时发生Mysql错误
$rule="**Mobile's** are not allowed into the room...................soon";
mysql_query("insert into table_name (rule) values('$rule')");
在执行该段不插入查询。我直接在Mysql中使用SQL选项尝试。那里显示错误。
任何建议..?
感谢
我试图在数据库中插入段落(包含中间单引号),如下所示。插入段落时发生Mysql错误
$rule="**Mobile's** are not allowed into the room...................soon";
mysql_query("insert into table_name (rule) values('$rule')");
在执行该段不插入查询。我直接在Mysql中使用SQL选项尝试。那里显示错误。
任何建议..?
感谢
使用
$rule = mysql_real_escape_string("**Mobile's** are not allowed into the room...................soon");
Thnku它的工作完美..! – 2013-05-09 13:05:41
尝试用: 的mysql_query( “插入到表名SET规则= '$规则'”);
错误,这不会解决问题。 – 2013-05-09 13:01:00
一些问题,让你觉得:
"
)意味着PHP?另外,也是最重要的,
Please, don't use mysql_*
functions in new code。他们不再维护and are officially deprecated。请参阅red box?请改为了解prepared statements,并使用PDO或MySQLi - this article将帮助您决定哪个。如果您选择PDO,here is a good tutorial。
在将值发送到Mysql数据库表之前,可以使用mysql_real_escape_string()。 请检查此链接http://php.net/manual/en/function.mysql-real-escape-string.php
$rule="**Mobile's** are not allowed into the room...................soon";
$rule1=mysql_real_escape_string($rule)
mysql_query("insert into table_name (rule) values('$rule1')");
来处理这样的事情最好的办法是使用PDO
分机或MySQLi
。它们旨在防止从SQL Injection
。
使用示例PDO
。
$rule = "**Mobile's** are not allowed into the room...................soon";
$stmt = $pdo->prepare("insert into table_name (rule) values(:rule)");
$stmt->execute(array(':rule' => $rule));
,我可以给解释是多么糟糕的查询断裂时的值包含单引号
最佳链接:
是“错误”可能是有趣的阅读,你不觉得吗?另外,用那个,我已经知道,你可以很容易地调试你的问题 – 2013-05-09 12:58:10
我建议你告诉我们错误 – Dima 2013-05-09 12:58:35