我正在使用表单将文本插入 MySQL 数据库。
当用户在表单中手动键入文本时,结果会完美地插入到数据库中。
但是,如果用户从另一个网页复制和粘贴文本,则会有隐藏的 p 标签与文本一起发送到数据库。标签在表单本身中是不可见的,但在提交时它们仍会发送到数据库。
如果我随后使用 MySQL SELECT 语句在网页上显示结果,则会显示不需要的标签,它们会破坏我的网页布局!
因此,当我从另一个网页复制和粘贴文本时,我只需要知道如何阻止将不需要的“p”“span”和“div”标签插入到我的 MySQL 数据库中。
有问题的网络表单是我正在构建的内容管理系统的一部分。从用户的角度来看,我需要表单是防弹的。现实情况是,用户很可能会从其他网站复制和粘贴文本,也可能从 word 文档中复制和粘贴文本,我需要确保在复制时不会将不需要的“p”“span”和“div”标签插入数据库并从第三方来源粘贴。
这是我的表单代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
<script type="text/javascript" src="http://www.achcreative.net/ckeditor/ckeditor.js"></script>
<link href="../elite.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!--Begin Main Menu -->
<?php include("includes/menu.inc.php"); ?>
<!--End Main Menu -->
<h2 class="subheaderh2">Insert New News Entry</h2>
<form method="post" action="insert_news.php">
<input name="publish" type="hidden" id="publish" value="publish" />
<table>
<tr><td><p>News Title:</p></td></tr>
<tr><td><input name="newstitle" type="text" size="43" id="newstitle"></td></tr>
<tr><td><p>News Article:</p></td></tr>
<tr><td><textarea name="newsarticle" cols="40" rows="10" id="newsarticle"></textarea>
<script type="text/javascript">
//<![CDATA[
// Replace the <textarea id="editor"> with an CKEditor
// instance, using default configurations.
CKEDITOR.replace( 'newsarticle',
{
toolbar :
[
[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],
]
});
//]]>
</script>
</td></tr>
<tr><td height="30" colspan="2"><input type="submit" value="Submit"></td></tr>
</table></form>
<p><a href="news_results.php">Return</a></p>
</body>
</html>
这是我的表单处理脚本代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
</head>
<body>
<h2 class="subheaderh2">News Entry Results</h2>
<?php
// create short variable names
$newstitle=$_POST['newstitle'];
$newsarticle=$_POST['newsarticle'];
$publish=$_POST['publish'];
if (!$newstitle || !$newsarticle)
{
echo '<p>You have not entered all the required details.<br />'
.'Please go back and try again.</p>'
.'<p><a href="javascript:history.go(-1)">Return</a></p>';
exit;
}
if (!get_magic_quotes_gpc())
{
$newstitle = addslashes($newstitle);
$newsarticle = addslashes($newsarticle);
}
$time = date("l jS F Y - g:iA");
// connect to the database
include('../connect-db.php');
/* Create the prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO news (id, newstitle, newsarticle, date, archive) values (NULL, ?, ?, NOW(), ?)")) {
/* Bind our params */
$stmt->bind_param('sss', $newstitle, $newsarticle, $publish);
/* Set our params */
$newstitle=$_POST['newstitle'];
$newsarticle=$_POST['newsarticle'];
$publish=$_POST['publish'];
/* Execute the prepared Statement */
$stmt->execute();
/* Echo results */
echo "{$newstitle}";
echo "<br />{$newsarticle}";
echo "Inserted into database on: ";
echo "$time";
echo "<br />";
echo "<br />";
echo '<a href="news_results.php">view results</a>';
/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
/* close our connection */
$mysqli->close();
?>
</body>
</html>
提前谢谢了
问候
安德鲁