【发布时间】:2010-10-28 04:49:37
【问题描述】:
我有一个页面应该显示一个带有复选框的表单,如果它被选中则回发,将结果存储在数据库中,然后在下次加载页面时将复选框显示为选中,并将其更新为未选中根据需要。
我知道我的解决方案有浮动“选中”并不理想,但它工作正常,我想坚持下去。
我遇到的问题是,虽然一条记录可能已正确插入到数据库中,但它在重新加载页面时既未显示为已检查,也未正确更新记录。
这是我的代码:
<?php
error_reporting(E_ALL);
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"]; else
if (isset($_POST["cmd"]))
$cmd = $_POST["cmd"]; else
die("Invalid URL");
if (isset($_GET["pk"])) {
$pk = $_GET["pk"];
}
$checkDeleted = "";
$checkNotice = "";
$checkWarrant = "";
$checkPromise = "";
$checkCompensation = "";
$checkBond = "";
$checkInjunction = "";
$checkActionpay = "";
$checkActionorder = "";
$checkCourtpayment = "";
$checkForeclosure = "";
$checkPenalty = "";
$checkboxes = $_POST['checkboxes'];
if (in_array('deleted', $checkboxes))
$checkDeleted = 'checked';
$con = mysqli_connect("localhost","user","pass", "db");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$con->set_charset("utf8");
$getformdata = $con->query("select ARTICLE_NO, deleted from STATUS where ARTICLE_NO = '$pk'");
while ($row = mysqli_fetch_assoc($getformdata)) {
$ARTICLE_NO = $row['ARTICLE_NO'];
$checkDeleted = $row['deleted'];
}
if($cmd=="submitinfo") {
if ($ARTICLE_NO == null) {
$statusQuery = "INSERT INTO STATUS VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("sssssssssssss", $pk, $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty);
$statusInfo->execute();
$statusInfo->close();
echo $pk;
echo $checkDeleted;
} else {
print_r($con->error);
}
} else if ($ARTICLE_NO == $pk) {
$statusQuery = "UPDATE STATUS SET deleted = ?, notice = ?, warrant = ?, promise = ?, compensation = ?, bond = ?, injunction = ?, actionpay = ?, actionorder = ?, courtpayment = ?, foreclosure = ?,penalty = ? WHERE ARTICLE_NO = ?";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("sssssssssssss", $checkDeleted, $checkNotice, $checkWarrant, $checkPromise, $checkCompensation, $checkBond, $checkInjunction, $checkActionpay, $checkActionorder, $checkCourtpayment, $checkForeclosure, $checkPenalty, $pk);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
}
}
if($cmd=="EditStatusData") {
echo '<link rel="stylesheet" href="style.css" type="text/css" />' . "\n";
echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
<h1>Editing information for Auction No: ".$pk."</h1>
<input type=\"checkbox\" name=\"checkboxes[]\" value=\"deleted\" ".$checkDeleted." />
<label for=\"deleted\">Löschung Ebay</label>
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
}
我已经尽可能地减少了这个,但不确定原因在哪里。
【问题讨论】: