フォームを使用してテキストを MySQL データベースに挿入しています。
ユーザーが手動でフォームにテキストを入力すると、結果がデータベースに完全に挿入されます。
ただし、ユーザーがたとえば別の Web ページからテキストをコピーして貼り付けると、テキストと共にデータベースに送信される非表示の p タグが存在します。タグはフォーム自体では表示できませんが、送信されるとデータベースに送信されます。
その後、MySQL SELECT ステートメントを使用して Web ページに結果を表示すると、不要なタグが表示され、Web ページのレイアウトが崩れます。
したがって、別の Web ページからテキストをコピーして貼り付けるときに、不要な 'p' 'span' および 'div' タグが MySQL データベースに挿入されないようにする方法を知る必要があります。
問題の Web フォームは、私が構築しているコンテンツ管理システムの一部です。ユーザーの観点からフォームを防弾にする必要があります。実際には、ユーザーは他の Web サイトや 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>
よろしくお願いします
よろしく
アンドリュー