【问题标题】:MySQL single quote insertion errorMySQL单引号插入错误
【发布时间】:2011-10-06 10:23:33
【问题描述】:

好的,我在一个从头开始制作的论坛中有一个表格。我正在使用 NBBC 为论坛解析 BBCode。这是代码。我的主要重点是将单引号转换为 html 实体。我也尝试了很多东西,包括 htmlentities() 。这是生成的错误消息:

ERROR [1064] 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 '' at line 1

这是当前代码。我提供了 2 个需要重新检查的代码。

add_topic.php(片段)

require_once("nbbc/nbbc.php");
$bbcode = new BBCode;
$topic=$_POST['topic'];
$detail=htmlspecialchars($_POST['detail']);
$c_detail=$bbcode->Parse($detail);
$name=$_POST['name'];
$c_name=htmlspecialchars($name, ENT_QUOTES);
$c_topic=htmlspecialchars($topic, ENT_QUOTES);
$datetime=date("d/m/y h:i:s"); //create date time

$sql=("INSERT INTO $tbl_name(topic, detail, name, datetime)VALUES('$c_topic', '$c_detail', '$c_name', '$datetime')");
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR [" . mysql_errno() . "] " . mysql_error();
}

add_answer.php

require_once("nbbc/nbbc.php");
$bbcode = new BBCode;
$a_name=$_POST['a_name'];
$a_subject=$_POST['a_subject'];
$a_answer=$bbcode->Parse($_POST['a_answer']);
$ac_name=htmlspecialchars($a_name, ENT_QUOTES);
$ac_subject=htmlspecialchars($a_name, ENT_QUOTES);
$datetime=date("d/m/y H:i:s"); // create date and time

$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_subject, a_answer, a_datetime)VALUES('$id', '$Max_id', '$ac_name', '$ac_subject', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<br />";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";

$tbl_name2="forum_question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR [" . mysql_errno() . "] " . mysql_error();
}

要重新澄清,我需要删除所有 html 标记和任何其他脚本标记,解析 BBCode,最后插入数据而不会出错。

【问题讨论】:

  • My main focus is to transform the single quotes into html entities - 是什么让您认为存在表示单引号的 HTML 实体?
  • @Codeboy 有一天Bobby Tables 在你的论坛上写了一篇文章,但在那之后你所有的成员表都不见了。发生了什么?
  • 在将“白手起家论坛”打开到 Internet 时应小心,您的代码似乎充满了安全漏洞。
  • 从你现在的代码中可以看出,伙计

标签: php mysql html


【解决方案1】:

试试 mysql_real_escape_string()。应该可以!

http://php.net/manual/en/function.mysql-real-escape-string.php

【讨论】:

  • 我知道有一个类似的答案,但你提供了第一个
【解决方案2】:

你需要使用 mysql-real-escape-string http://php.net/manual/en/function.mysql-real-escape-string.php - 不是 htmlspecialchars

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 2015-08-07
    • 2018-08-18
    • 2018-07-16
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多