addCategory.php
<?php
$parentId = isset($_GET['parentId']) ? $_GET['parentId'] : 0;
$categoryName = $categoryDescription = "";
$fail = "";
if (isset($_POST['submit'])) {
if (isset($_POST['categoryName']))
$categoryName = fix_string($_POST['categoryName']);
if (isset($_POST['categoryDescription']))
$categoryDescription = fix_string($_POST['categoryDescription']);
$hidParentId = $_POST['hidParentId'];
}
$fail = validate_category_name($categoryName);
$fail .= validate_category_description($categoryDescription);
echo "<html><head><title>An Example Form</title>";
if ($fail == "") {
echo "success";
header("Location: processCategory.php?action=add&categoryName=$categoryName&categoryDescription=$categoryDescription&hidparentId=$hidParentId");
exit;
}
// Now output the HTML and JavaScript code
?>
<!-- The HTML section -->
<style>.signup { border: 1px solid #999999;
font: normal 14px helvetica; color:#444444; }</style>
<script type="text/javascript">
function validate(form)
{
fail = validateCategoryName(form.categoryName.value)
fail += validateCategoryDescription(form.categoryDescription.value)
if (fail == "") return true
else { alert(fail); return false }
}
</script></head><body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Add Category</th>
<?php
if (isset($_POST['submit'])) {
?>
<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i><?php echo $fail ?></i></font></p>
</td></tr>
<?php
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?parentId=<?php echo $parentId; ?>"
onSubmit="return validate(this)">
<tr><td>Category Name</td><td><input type="text" maxlength="32"
name="categoryName" value="<?php echo $categoryName; ?>" /></td>
</tr><tr><td>Category Description</td><td><input type="text" maxlength="32"
name="categoryDescription" value="<?php echo $categoryDescription; ?>" /></td>
<input type="hidden" name="hidparentId" value="<?php echo $parentId; ?>" />
</tr><tr><td colspan="2" align="center">
<input type="submit" name="submit" value="ok" /></td>
</tr></form></table>
<!-- The JavaScript section -->
<script type="text/javascript">
function validateCategoryName(field) {
if (field == "") return "No name entered.\n"
return ""
}
function validateCategoryDescription(field) {
if (field == "") return "No description entered.\n"
return ""
}
</script></body></html>
<?php
// Finally, here are the PHP functions
function validate_category_name($field) {
if ($field == "") return "No name entered<br />";
return "";
}
function validate_category_description($field) {
if ($field == "") return "No description entered<br />";
return "";
}
function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>
processCategory.php
<?php
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'add' :
addCategory();
break;
case 'modify' :
modifyCategory();
break;
case 'delete' :
deleteCategory();
break;
default :
// if action is not defined or unknown
// move to main category page
header('Location: index.php');
}
/*
Add a category
*/
function addCategory() {
$name = $_GET['categoryName'];
$description = $_GET['categoryDescription'];
$parentId = $_GET['hidparentId'];
$sql = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description)
VALUES ($parentId, '$name', '$description')";
$result = dbQuery($sql) or die('Cannot add category' . mysql_error());
header('Location: index.php?catId=' . $parentId);
}
function modifyCategory() {
}
function deleteCategory() {
}
?>
ここで、POST を介してユーザー入力を取得し、その POST データを同じ .php ファイルに送信することに注意してください。次に、検証後にそれらのデータを別の .php ファイルに送信します。THRU GET .....それらの GET データをDB 次に、それらのデータを挿入した後、別のページにリダイレクトします。
データベースの状態を変更する場合は、GET ではなく POST を使用する必要があることを読みました。GET データが URL に表示され、ユーザーが URL のデータベースの状態を変更できるため、これは悪いことだと思います。もう 1 つは、POST を使用している場合に更新をクリックすると、ブラウザーは、もう一度同じ方法を使用しますが、GET を使用すると、ブラウザーは警告を表示しないため、同じデータを 2 回投稿することになります。
私のコードでは、データが挿入されるとすぐに別のページにリダイレクトされるため、ユーザーが URL を操作して DB を変更することはできません。また、更新の問題はここでは問題になりません。
入力を処理する場所を分離したいのですが、それがprocessCategory.phpです。私は初心者ですが、私が正しいことをしているかどうか教えてください。