使用PhP的论坛评论部分

使用PhP的论坛评论部分,php,database,forms,quotes,forum,Php,Database,Forms,Quotes,Forum,我只是一个新手程序员,我正在制作一个论坛来提高我的技能,但我现在遇到了一个问题。 这就是代码的问题: 问题是:我们不能使用单引号,我们希望使用单引号和双引号 这是它进入数据库的地方: // get data that sent from form $topic=$_POST['topic']; $detail=$_POST['detail']; $name=$_SESSION['display']; $email=$_POST['email']; 这是表格的一部分: <tr>

我只是一个新手程序员,我正在制作一个论坛来提高我的技能,但我现在遇到了一个问题。 这就是代码的问题:

问题是:我们不能使用单引号,我们希望使用单引号和双引号

这是它进入数据库的地方:

// get data that sent from form 
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_SESSION['display'];
$email=$_POST['email'];
这是表格的一部分:

<tr>
    <td width="14%"><strong>Topic</strong></td>
    <td width="2%">:</td>
    <td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
    <td valign="top"><strong>Detail</strong></td>
    <td valign="top">:</td>
    <td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>

)

您正在运行一种叫做“sql注入”的东西,这是一个您应该关心的严重问题

请阅读并使用此函数转义输入数据的特殊字符

似乎您有这样一个查询:

$query = "INSERT INTO tbl (`topic`) VALUES ('$topic');"
因此,您生成的查询字符串可能如下

$topic = "foobar";
$query = "INSERT INTO tbl (`topic`) VALUES ('$topic');"
echo $query;
将导致:

INSERT INTO tbl (`topic`) VALUES ('foobar');
这很好。但是:

$topic = "f'); delete from tbl;";
$query = "INSERT INTO tbl (`topic`) VALUES ('$topic');"
echo $query;
将返回:

INSERT INTO tbl (`topic`) VALUES('f'); delete from tbl;
这不是你希望发生的事


为了提高PHP编程技能,您应该阅读PDO文档()并进一步了解sql语句中参数的使用:)

我猜您使用的是mysql函数,因此,更好的解决方案是注意该页面上的红色大框,按照它的建议去做。所以我必须对导入的东西做一个变量?我还修改了上面的代码,所以你可以看到它是如何进行的,我没有什么要添加到我的答案;请跟随我的链接。您的代码与预期的一样。使用mysql_real_escape_字符串或-更好-阅读PDO。如果你高兴,请接受我的回答。
$topic = "f'); delete from tbl;";
$query = "INSERT INTO tbl (`topic`) VALUES ('$topic');"
echo $query;
INSERT INTO tbl (`topic`) VALUES('f'); delete from tbl;