插入段落时出现Mysql错误

时间:2013-05-09 12:56:03

标签: php mysql

我正在尝试将段落(其中包含单个引号)插入到数据库中,如下所示。

$rule="**Mobile's** are not allowed into the room...................soon";


mysql_query("insert into table_name (rule) values('$rule')");

执行段落未插入的查询时。我已经使用SQL选项在Mysql中直接尝试过了。它显示错误。

有什么建议吗??

由于

5 个答案:

答案 0 :(得分:1)

有些问题可以让你思考:

  1. 双引号(")在PHP中意味着什么?

  2. 它们对包含变量的字符串有什么影响?

  3. 什么是“输入卫生”?

  4. 什么是“角色逃避”?

  5. 什么是PDO?

  6. 此外,最重要的是:

    Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:1)

处理此类问题的最佳方法是使用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));

我能给出的最佳链接解释了当值包含单引号时查询的中断有多糟糕:

答案 2 :(得分:0)

使用

$rule = mysql_real_escape_string("**Mobile's** are not allowed into the room...................soon");

答案 3 :(得分:0)

您可以在将值发送到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')");

答案 4 :(得分:-1)

尝试: mysql_query(“insert into table_name SET rule ='$ rule'”);