【问题标题】:mysql not updating from php formmysql没有从php表单更新
【发布时间】:2009-05-25 16:12:32
【问题描述】:

我有一个非常简单的 PHP 表单,它显示了一个复选框,如果它被选中,它将存储在数据库中。这适用于初始插入,但不适用于更新。我已经测试了 $saleid 等于 $pk 并且它没有进入 if 分支进行更新的案例......为什么?

<?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"]; }

$checkfield = "";

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

if (in_array('field', $checkboxes)) $checkfield = 'checked';

$con = mysqli_connect("localhost","user","", "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 saleid, field from STATUS where saleid = '$pk'");
$saleid = "";
while ($row = mysqli_fetch_assoc($getformdata)) {
    $saleid = $row['saleid'];
    $checkfield = $row['field'];
}

if($cmd=="submitinfo") {
    if ($saleid == null) {
       $statusQuery = "INSERT INTO STATUS VALUES (?, ?)";
        if ($statusInfo = $con->prepare($statusQuery)) {
                $statusInfo->bind_param("sssssssssssss", $pk, $checkfield);
                $statusInfo->execute();
                $statusInfo->close();
        } else {
                print_r($con->error);
        }
    } else if ($saleid == $pk) {
        $blah = "what";
        $statusQuery = "UPDATE STATUS SET field = ? WHERE saleid = ?";
        if ($statusInfo = $con->prepare($statusQuery)) {
                $statusInfo->bind_param("ss", $checkfield, $pk);
                $statusInfo->execute();
                $statusInfo->close();
        } else {
                print_r($con->error);
     }  
    }
}
if($cmd=="EditStatusData") {
    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=\"field\" ".$checkfield." />
                        <label for=\"field\">Test</label>
                        <br />
                        <input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
                        <input name=\"Submit\" type=\"submit\" value=\"submit\" />
        </form>";
}
?>

【问题讨论】:

    标签: php sql mysql html


    【解决方案1】:

    我创建了一个表格并运行了你的代码,它对我来说很好用

    它“看起来”不像更新正常工作的原因是因为您正在阅读 来自数据库的 $saleid 和 $checkfield 然后构建一个更新语句,将相同的两个值放回数据库中

    这可能不是你想要做的

    这里的这一行将 $checkfield 设置为 'checked',

     if (in_array('field', $checkboxes)) $checkfield = 'checked';
    

    然后你从数据库中设置 $checkfield (覆盖值 'checked' )

    while ($row = mysqli_fetch_assoc($getformdata)) {
       $saleid = $row['saleid'];
       $checkfield = $row['field'];
    

    然后将checkfield的原始值写回数据库

    $statusInfo->bind_param("ss", $checkfield, $pk);
    

    【讨论】:

      【解决方案2】:

      不确定是否可以混合使用 GET 和 POST 类型的请求

      也许改变这个,以便 pk 作为隐藏字段传回?

       echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
      

      例如,有点像这样

      echo "<form name=\"statusForm\" action=\"test.php\" method=\"post\" enctype=\"multipart/form-data\">
      <input type=\"hidden\" name=\"pk\" value=\"".$pk."\">
      

      【讨论】:

      • 还要记住如果复选框没有打勾,它不会在php中的checkboxes[]数组中返回值
      【解决方案3】:

      你的 HTML 应该是这样的:

          <form id="aform" action="thisform.php" method="post">
             <input type="checkbox" name="agree" value="yes" />
             <input type="hidden" name="secret" value="shhh" />
             <input type="submit" value="do it" />
          </form>
      

      如果您这样做,请使用上述内容:

        print_r($_POST);
      

      你会得到一个数组,要么有 [agree] => 'yes' 要么什么都没有,这取决于他们是否选中了该框,因此除非你有大量的框,否则无需放置数组括号。

      至于 SQL 部分,我建议将列设为单个整数类型,其中可以有 0 或 1。0 表示未选中,1 表示选中。对于插入,您可以执行以下操作:

         $check_value = ($_POST['agree'] == 'yes') ? 1 : 0;
         $secret_stuff = $_POST['secret'];
         mysqli_query("Insert INTO sales_table (secret_column, agree_column)
                       VALUES ('$secret_stuff', '$check_value')");
      

      这将使您的复选框进入表格。要把它拿出来,你应该去:

        $results = mysqli_query("SELECT * from sales_table where secret_column = $secret_stuff")
      
        while($row = mysqli_fetch_assoc($results)) {
          $checked = ($row['agree_column'] == 1) ? "checked=\"checked\"" : "";
          $secret_stuff = $row['secret_column];
         }
      
         ?>
         <form action=blah method=post id=blah>
         <input type="checkbox" name="agree" value="yes" <?php echo $checked;?> />
         </form>
      

      抱歉,最后失去了动力。但这涵盖了前端和后端。使用 1/0 开关,如果它是 1,只需将 $checked 之类的变量设置为“checked='checked'”。

      【讨论】:

      • 我现在的做法很好,我的 html...唯一的问题是更新语句没有被调用
      【解决方案4】:

      除非isset($_GET["pk"]),否则您不会设置 $pk 变量,但稍后您仍在查询中使用它。这不是一个好主意,因为根据其他情况,这可能会导致错误。你希望你的逻辑看起来像这样:

      if pk is not set in form
          insert new record
          deal with error if insert failed
      else
          update existing record
          check update count and deal with error if 0 records were updated
              (perhaps by doing an insert of the missing record)
      end
      

      【讨论】:

      • 表单总是以 pk 作为我的 ajax 应用程序的参数调用,所以这是设计行为。目前唯一的问题是没有进入更新分支。
      • 我想我的主要观点是你可以简化你的逻辑,这样你就必须跟踪更少的不同状态集进行调试......
      【解决方案5】:

      顺便说一句,看起来 mysql REPLACE 函数会派上用场。

      此外,当未选中复选框时,该值可能是一件棘手的事情。我编写了一个函数,如果设置了发布的值,则将值设置为 1,如果没有设置,则设置为零......

      function checkbox_value($name) {
          return (isset($_POST[$name]) ? 1 : 0);
      }
      

      您可以通过该查询运行已发布的复选框值,并且始终得到 1 或 0。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多