【问题标题】:problem maintiaing form state with mysql and php使用 mysql 和 php 维护表单状态的问题
【发布时间】: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>";
}

我已经尽可能地减少了这个,但不确定原因在哪里。

【问题讨论】:

    标签: php mysql html ajax forms


    【解决方案1】:

    如果我理解你的问题是正确的替换

    $checkboxes = $_POST['checkboxes'];

    与

    $checkboxes = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());

    我想会解决你的问题。 (我没有运行脚本进行测试,但希望没有错误)

    完整示例:

    <?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 = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());
    
    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>";
    }
    ?>
    

    【讨论】:

    • 为什么你改行解决了问题?此外,更新分支仍然没有功能
    • 因为没有完成 $_POST 数组时没有分配即时数组值。你也可以检查你的 "if (isset($_GET["cmd"])) $cmd = $_GET["cmd"]; else if (isset($_POST["cmd"])) $cmd = $_POST[ "cmd"]; else die("无效的 URL"); " 再次验证!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2013-06-15
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多