我有一个表单,可以将每个项目保存到我的数据库中。我有不同选项的复选框,例如类别和子类别,它们保存为数组。保存到数据库中时,所有这些都能很好地工作。但是我很难弄清楚如果类别错误,如何勾选复选框。我尝试了很多东西,最后放弃了。我希望有人能指出我正确的方向。
在我的编辑页面上,我有两个代码块:一个用于获取数据库中的信息,另一个用于POST更新的信息。
这是GET代码:
<?php
if (isset($_GET['sid'])) {
$targetID = $_GET['sid'];
$sql = mysql_query("SELECT * FROM scents WHERE id='$targetID' LIMIT 1");
$scentCount = mysql_num_rows($sql); // count the output amount
if ($scentCount > 0) {
while($row = mysql_fetch_array($sql)){
$scent_name = stripslashes($row["scent_name"]);
$can_compat = $row["can_compat"];
$gel_compat = $row["gel_compat"];
$scent_description = stripslashes($row["scent_description"]);
$sug_color = stripslashes($row['sug_color']);
$soap_compat = $row["soap_compat"];
$phthalate_free = $row["phthalate_free"];
$category = stripslashes($row["category"]);
$subcategory = stripslashes($row["subcategory"]);
}
} else {
$msg_to_user = "Sorry, That scent doesn't exist.";
}
}
?>
这是POST代码:
(我没有更新类别和子类别检查,因为我无法让它们工作,所以当我能搞清楚时会添加它)
<?php
if (isset($_POST['scent_name'])) {
$sid = mysql_real_escape_string($_POST['thisID']);
$allowed = '<div><p><span><strong><em><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>';
$scent_name = mysql_real_escape_string($_POST['scent_name']);
$scent_description = mysql_real_escape_string(strip_tags($_POST['scent_description'], $allowed));
$can_compat = mysql_real_escape_string($_POST['can_compat']);
$gel_compat = mysql_real_escape_string($_POST['gel_compat']);
$soap_compat = mysql_real_escape_string($_POST['soap_compat']);
$phthalate_free = mysql_real_escape_string($_POST['phthalate_free']);
$sug_color = mysql_real_escape_string($_POST['sug_color']);
if(isset($_POST['category'])) {
$category = implode(", ", $_POST['category']);
} else {
$category = "";
}
if(isset($_POST['subcategory'])) {
$subcategory = implode(", ", $_POST['subcategory']);
} else {
$subcategory = "";
}
$sql = mysql_query("UPDATE `scents` SET `scent_name`='$scent_name', `scent_description`='$scent_description', `can_compat`='$can_compat', `gel_compat`='$gel_compat', `soap_compat`='$soap_compat', `phthalate_free`='$phthalate_free', `sug_color`='$sug_color' WHERE `id`='$sid'");
header("location: admin_scent_manage.php");
exit();
}
?>
以下是部分表单代码:
<tr>
<td align="right">Holidays: <label for="category">
<input type="checkbox" id="category[]" name="category[]" value="Holiday" >
</label>
</td>
<td align="left">
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Christmas" > Christmas<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Valentines" > Valentines Day<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Thanksgiving" > Thanksgiving<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="StPatrick\'s" > St. Patrick's Day<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Easter" > Easter<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="4th" > Fourth of July<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Wedding" > Wedding<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Mother\'s Day" > Mother's Day<br />
<input type="checkbox" id="subcategory[]" name="subcategory[]" value="Father\'s Day" > Father's Day
</td>
</tr>
如果某个类别或子类别在特定项目的数组中,我需要弄清楚如何确保选中正确的复选框,这样如果我需要取消选中它们或检查其他框,它会相应地更新。其他一切都完美无缺。我无法弄清楚复选框部分。我已经尝试了所有我能找到的东西,没有任何工作,所以我现在就让他们不要更新。