多个select和add more字段不起作用

gg0vcinb  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(357)

这是我第一次使用多个select选项并在表单中添加更多的字段函数。我有这个表格,我可以输入多个序列号,也可以添加多个配件和相应的数量。但第一次选择的序列号和配件(和数量)保存在我的数据库中。
这是我的html表单:

<form id="form1" name="form1" method="post" action="requisitionForm_process.php" onsubmit="return validateForm()" >
     <div class="form-group">
       <label for="DONO">DO. Number: </label>
        <input class="form-control" id="DONO" name="DONO" placeholder="Place DO. Number Here" type="text"/>
     </div> 
     <div class="form-group">
       <label for="requestby">Request By: </label>
       <select class="form-control" id="empCode" name="empCode">
          <?php while ($row = mysqli_fetch_array($resultR)):;?>
          <option value = <?php echo $row ['empCode']; ?>><?php echo $row ['empCode']; echo ":"; echo $row ['empName'];?></option> 
          <?php endwhile;
          ?>
        </select>
     </div> 
     <div class="form-group">
       <label for="requestby">Customer: </label>
       <select class="form-control" id="customerCode" name="customerCode">
          <?php while ($row = mysqli_fetch_array($resultC)):;?>
          <option value = <?php echo $row ['customerCode']; ?>><?php echo $row ['customerCode']; echo ":"; echo $row ['customerName'];?></option> 
          <?php endwhile;
          ?>
        </select>
     </div> 
     <div class="form-group"> <!-- Date input -->
        <label class="control-label" for="dateOUT">Date:</label>
        <input class="form-control" id="dateOUT" name="dateOUT" placeholder="DD/MM/YYY" type="text"/>
      </div>
     <div class="form-group">
       <label for="categoryCode">Category: </label>
       <select class="form-control" id="categoryCode" name="categoryCode">
        <option value="new"> New Installation</option>
        <option value="re">Re-installation</option>
      </select>
     </div> 
     <div class="form-group">
       <label for="categoryCode">Product: </label>
       <select class="form-control" id="productCode" name="productCode">
          <?php while ($row = mysqli_fetch_array($result)):;?>
          <option value = <?php echo $row ['productCode']; ?>><?php echo $row ['productCode']; echo ":"; echo $row ['productName'];?></option> 
          <?php endwhile;
          ?>
        </select>
     </div> 
     <div class="form-group">
         <label for="serialNumber">Serial Number:</label><br>
         <select id="serialNumber" name="serialNumber[]" multiple class="form-control" >
          <?php while ($row = mysqli_fetch_array($resultSe)):;?>
          <option value = <?php echo $row ['serialNumber']; ?>><?php echo $row ['serialNumber'];?></option> 
          <?php endwhile;?>   
          </select>
     </div> 
     <table class="table table-bordered" id="item_table">
      <tr>
       <th>Select Accessories</th>
       <th>Enter Quantity</th>
       <th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
      </tr>
     </table>
  <div class="form-group">
    <input type="button" class="btn"  id="checkBtn" name="return" value="Back" onClick="javascript: window.history.back(-1)";>
    <input class="btn btn-primary" type="reset" value="Reset">
    <input align="right" type="submit" class="btn btn-info" value="Submit Button">
  </div>
</div>
</form>

这是我的php过程:

<?php

if(isset($_POST["serialNumber"]))
{
  $serialNumber = '';
  foreach($_POST["serialNumber"] as $row)
  {
    $serialNumber .= $row . ', ';
  }
    $serialNumber = substr($serialNumber, 0, -2);

if(isset($_POST["categoryCode"]))
{
    $connect = new PDO("mysql:host=localhost;dbname=ims", "root", "");
    $DONO = $_POST["DONO"];
    $empCode = $_POST["empCode"];
    $customerCode = $_POST["customerCode"];
    $categoryCode = $_POST["categoryCode"];
    $productCode = $_POST["productCode"];
    $dateOUT = date('Y-m-d', strtotime($_POST['dateOUT']));
    for($count = 0; $count < count($_POST["accID"]); $count++)
    {  
      $query = "INSERT INTO requisition (DONO, empCode, customerCode, dateOUT, categoryCode, productCode, serialNumber, quantityAcc, accID) VALUES (:DONO, :empCode, :customerCode, :dateOUT, :categoryCode, :productCode, :serialNumber, :quantityAcc, :accID) ";
      $statement = $connect->prepare($query);
      $statement->execute(
       array(
       ':DONO'   => $DONO,
       ':empCode'   => $empCode,    
       ':customerCode'   => $customerCode,
       ':dateOUT'   => $dateOUT,
       ':categoryCode'   => $categoryCode,
       ':productCode'   => $productCode,
       ':serialNumber'   => $serialNumber,
       ':quantityAcc' => $_POST["quantityAcc"][$count], 
       ':accID'  => $_POST["accID"][$count]
        )
      );
     }
 $result = $statement->fetchAll();
  if(isset($result))
  {
  echo 'ok';
  }
}
}

?>
3pmvbmvn

3pmvbmvn1#

if(isset($_POST["test"]))
{
  $serialNumber = '';
  foreach($_POST["test"] as $row=>$value)
  {
    $serialNumber .= $value . ', ';
  }
    $serialNumber = substr($serialNumber, 0, -2);
}

在我无法使用html之前,用这个替换你的循环。

相关问题