通过foreach插入数据库

时间:2016-09-11 16:10:15

标签: php mysql checkbox

所以我在下面有这些代码。第一个是循环,它回应了数据库中的所有需求并以复选框形式写入。所以我所做的是以数组形式命名它 requirementItems []

下一个代码是关于检查和插入的。如果选中该复选框,则$state变量的值为1其他,0

我的问题是,当我试图取消选中复选框时,它不会记录到数据库中。但如果选中它,它会成功插入数据。

第一

<?php
      $query = $db->prepare("SELECT * FROM tbl_requirements WHERE reqStatus = 1");
      $query->execute();
        while($result = $query->fetch(PDO::FETCH_ASSOC)){
          $requirementID      =  $result['reqID'];
          $requirementName    =  $result['reqName'];
          echo " 

            <div class='row'>
              <div class='col-md-5'>
              <div class='checkbox'>
                <label>
                  <input type='checkbox'value='$requirementID' title='$requirementID' name='requirementItems[]'>
                  $requirementName
                </label>
             </div>
             </div>
            </div>
          ";

        }
    ?>

SECOND

<?php
if(isset($_POST['passSubmit'])){
$rsrvReqID                = trim($_POST['reqID']);
$customerID           = $_GET['cusID'];
$reservedProperty     = $_GET['propertyID'];  
$paymentID            = $_GET['paymentID'];
$requirementItems     = $_POST['requirementItems'];

  foreach($requirementItems as $reqItem) {
    $state = (isset($reqItem)) ? 1 : 0;

      $query = $db->prepare("INSERT INTO tbl_rsrvrequirements (rsrvReqID, customerID, propertyID, paymentID, reqID, rsrvReqState) 
        VALUES (:rsrvReqID, :customerID, :reservedProperty, :paymentID, :reqID, :state)");
      $query->execute(array(
        ":rsrvReqID"            => $rsrvReqID,
        ":customerID"           => $customerID,
        ":reservedProperty"     => $reservedProperty,
        ":paymentID"            => $paymentID,
        ":reqID"                => $reqItem,
        ":state"                => $state

        ));
      $reqNEW = str_replace('REQ','',$rsrvReqID);
      $requireID = str_pad(trim($reqNEW) +2,7,0,STR_PAD_LEFT);
      $rsrvReqID = 'REQ'.$requireID;
  }//foreach()
  echo"<meta http-equiv='refresh' content='0; url=summary.php?reqID=$rsrvReqID' >";
}//if(isset())
 ?>

0 个答案:

没有答案