2015-10-21 64 views
0

我試圖讓用戶指定他們想要添加到客戶購買的訂單表格中的行數。這使得用戶可以根據需要爲購買產品提供儘可能多的行,而不必擁有一個列表。我的功能正常運行,如果您輸入3並提交,它將爲您提供三行以輸入產品訂單信息。 我遇到的問題是我在哪裏填充帶有產品ID和名稱的列表框供用戶選擇。它填充第一行的列表框,但以下列表框僅獲取「 - 」而不是$ row []值。它似乎不再傳遞sql語句,爲什麼?一次填充多個列表框的SQL語句

這是我遇到的問題與功能在我的代碼區域:

<? 
      if (isset($_POST['update'])) 
      { 
       //Execute this code if the update button is clicked. 
       $num = $_POST['rows']; 

       for ($i=0; $i<$num; $i++) { ?> 

        <tr> 
         <td class="inputCol2"> 
          <select name="'product<?= $i ?>"> 
           <option value="selectProduct">Select Product</option> 
           <!-- Populate listbox with Product ID and Product Name --> 
           <? 
           do { ?> 

            <option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option> 

           <? } while($row = mysqli_fetch_array($result)) ?> 

          </select> 
         </td> 
         <td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td> 
         <td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td> 
         <td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td> 
        </tr> 
       <? } ?> 

這是我的全部代碼:

<? 
     connectDB(); 

     $sql = "SELECT * FROM product"; 
     $sql2 = "SELECT DISTINCT emp_id, emp_fname, emp_lname FROM employee"; 
     $sql3 = "SELECT DISTINCT status_id FROM salesorder ORDER BY status_id asc"; 
     $sql4 = "SELECT * FROM salesorder ORDER BY order_id desc"; 

     $result = mysqli_query($db, $sql) or die("SQL error: " . mysqli_error()); 
     $result2 = mysqli_query($db, $sql2) or die("SQL error: " . mysqli_error()); 
     $result3 = mysqli_query($db, $sql3) or die("SQL error: " . mysqli_error()); 
     $result4 = mysqli_query($db, $sql4) or die("SQL error: " . mysqli_error()); 

     $row = mysqli_fetch_array($result); 
     $row2 = mysqli_fetch_array($result2); 
     $row3 = mysqli_fetch_array($result3); 
     $row4 = mysqli_fetch_array($result4); 

     ?> 

    <div id="order-wrap"> 
     <form method="post" action="order.php"> 
      <table class="orderInfo"><br> 
       <tr> 
        <th class="textCol">Product Rows:</th> 
        <td class="inputCol"><input type="text" name="rows"></td> 
        <td><input class="update" type="submit" name="update" value="Update"></td> 
        <td class="inputCol"></td> 
       </tr> 
      </table> 
     </form><!-- Order Rows --> 
     <form class="orderform" action ="order-report.php" METHOD = "post"> 
      <h2>Order Form</h2> 
      <h3>Piedmont Furnishings</h3> 
      <img id="couch-img" src="couch.jpg" alt="couch"> 
      <table class="orderInfo"> 
       <tr> 
        <th class="textCol">Order Number:</th> 
        <td class="inputCol"><input type="text" name="orderNumber" value="<?= $row4[0] + 1; ?>" disabled></td> 
        <th class="textCol">Order Date:</th> 
        <td class="inputCol"><input type="text" name="orderDate" value="<?= date("Y-m-d") ?>"></td> 
       </tr> 
       <tr> 
        <th class="textCol">Customer:</th> 
        <td class="inputCol"><input type="text" name="customer"></td> 
        <td class="textCol"></td> 
        <td class="inputCol"></td> 
       </tr> 
       <tr> 
        <th class="textCol">Sales Agent:</th> 
        <td class="inputCol"> 
         <select name="salesAgent"> 
          <option value="selectAgent">Select One</option> 
          <!-- Populate listbox with Sales Agents ID --> 
          <? 
          do { ?> 

           <option value="<?= $row2[0]; ?>"><?= $row2[1] . " " . $row2[2]; ?></option> 

          <? } while($row2 = mysqli_fetch_array($result2)) ?> 

         </select> 
        </td> 
        <th class="textCol">Order Status:</th> 
        <td class="inputCol"> 
         <select name="orderStatus"> 
          <option value="selectStatus">Select One</option> 
          <!-- Populate listbox with Status ID --> 
          <? 
          do { ?> 

           <option value="<?= $row3[0]; ?>"><?= $row3[0] ?></option> 

          <? } while($row3 = mysqli_fetch_array($result3)) ?> 

         </select> 
        </td> 
       </tr> 
      </table> 

     <!-- Where the product rows input show go ??? --> 
     <table class="bottomTable"> 
      <tr> 
       <th class="textCol">Product</th> 
       <th class="textCol">Quantity</th> 
       <th class="textCol">Unit Price</th> 
       <th class="textCol">Total Price</th> 
      </tr> 
     <? 
      if (isset($_POST['update'])) 
      { 
       //Execute this code if the update button is clicked. 
       $num = $_POST['rows']; 

       for ($i=0; $i<$num; $i++) { ?> 

        <tr> 
         <td class="inputCol2"> 
          <select name="'product<?= $i ?>"> 
           <option value="selectProduct">Select Product</option> 
           <!-- Populate listbox with Product ID and Product Name --> 
           <? 
           do { ?> 

            <option value="<?= $row[0]; ?>"><?= $row[0] . " - " . $row[2]; ?></option> 

           <? } while($row = mysqli_fetch_array($result)) ?> 

          </select> 
         </td> 
         <td class="inputCol2"><input type="text" name="'quantity<?= $i ?>" ></td> 
         <td class="inputCol2">$<input type="text" name="'unit<?= $i ?>" value=""></td> 
         <td class="inputCol2">$<input type="text" name="'total<?= $i ?>" value="" ></td> 
        </tr> 
       <? } ?> 
        <tr> 
         <td class="textCol"></td> 
         <td class="textCol"></td> 
         <td class="textCol">Total Order:</td> 
         <td class="inputCol2">$<input type="text" name="totalfinal"></td> 
        </tr> 
        <input class="submit" type="submit" value="Submit" name="orderSubmit"/> 
       </table> 
      </form> 
      <? } else {?> 
        <tr> 
         <td class="textCol"></td> 
         <td class="textCol"></td> 
         <td class="textCol">Total Order:</td> 
         <td class="inputCol2">$<input type="text" name="totalfinal"></td> 
        </tr> 
        <input class="submit" type="submit" value="Submit" name="orderSubmit"/> 
       </table> 
      </form> 
      <? } ?> 
     <? 
      mysqli_free_result($result); 
      mysqli_close($db); 
     ?> 
    </div> 

回答

1

與您的代碼問題對於第一次迭代while($row = mysqli_fetch_array($result))$result的內部指針達到了最後...因此,對於下一次迭代$i=1$result中沒有任何內容,但是當您使用do-while循環時,循環必須至少運行一次,$row[0] & $row[2]空,所以你只能得到「 - 」。要解決您需要稍微更改代碼的問題。

刪除此行$row = mysqli_fetch_array($result);

,並添加

$options = '<option value="selectProduct">Select Product</option>'; 

while($row = mysqli_fetch_array($result,MYSQLI_NUM)){ 

    $options .= '<option value="'.$row[0].'">'.$row[0].' - '.$row[1].'</option>'; 
} 

然後像這裏面更改循環:

<td class="inputCol2"> 
    <select name="'product<?= $i ?>"> 
    <?php 
     echo $options; 
    ?> 
    </select> 
</td> 
+0

謝謝!這工作。從未考慮過使用MYSQLI_NUM。 – Haley