0

我的表格包含字段 X:标题、内容、时间。

通过 htmlform 我将数据上传到此表中。html是:

<form action="process2.php" method="post" enctype="multipart/form-data">
Headline: <input name="headline" type="text" size="100" /><br /><br />
Content:<textarea name="content" cols="100" rows="10" placeholder="Content here">
</textarea><br /><br />
</form>

process2 php脚本的一部分:

$headl = mysql_real_escape_string($_POST['headline']);
$headline=htmlspecialchars("$headl", ENT_QUOTES);

$cont=$_POST['content'];
$cont = str_replace("&lt;", "<", $cont);
$cont = str_replace("&gt;", ">", $cont);
$content=htmlentities($cont,ENT_QUOTES);

$sql="INSERT INTO X(`headline`, `content`, `date`) VALUES ('$headline','$content',NOW())";

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

问题:标题和日期被插入。而内容仍然是空的......

我也在内容上尝试了 mysql_real_escape_string 但没有用。为什么内容是空的?……有什么解决办法吗?

4

1 回答 1

0

请试试这个,它应该适用于您的情况:

// htmlspecialchars Must with ENT_QUOTES, it actually make mysql_real_escape_string useless, but mysql_real_escape_string for safe anyway.

$headline = mysql_real_escape_string(htmlspecialchars($_POST['headline'], ENT_QUOTES));
$content = mysql_real_escape_string(htmlspecialchars($_POST['content'], ENT_QUOTES));

$sql = "INSERT INTO X(`headline`, `content`, `date`) VALUES ('{$headline}', '{$content}', NOW())";

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

要显示,您可以从 mysql 加载数据,并为每个执行 htmlspecialchars_decode。

更简单的方法是,保存时不要做 htmlspecialchars,就像这样:

$headline = mysql_real_escape_string($_POST['headline']);
$content = mysql_real_escape_string($_POST['content']);

在 mysql_real_escape_string 之后,数据对于 mysql 来说应该是安全的,因为所有已知的注入技巧都会被过滤掉。您只需要关心它自己的 html 内容。哦,这两个内容的长度都不能超过字段的字符限制。

于 2013-04-28T10:48:22.787 回答