首页 > 解决方案 > 如何使用相同的发票编号调用 Mysql 表中每列的不同值?

问题描述

我有一个表purchase_details,其中一些值具有相同的invoice_no. 现在我需要每个数据都相同,并将它们输出到一个长度invoice_no相同的输入字段中。invoice_no

由于我是 PHP 新手,我尝试使用数组,但它显示了最后一行的字母。

这是我要显示的表格数据

<table align="center" style="width:800px;">
      <thead>
             <tr>
                  <th>#</th>
                  <th style="text-align:center;">Item Name</th>
                  <th style="text-align:center;">Total Quantity</th>
                  <th style="text-align:center;">Quantity</th>
                  <th style="text-align:center;">Buy Price</th>
                  <th>Total</th>
             </tr>
       </thead>
       <tbody id="invoice_item">
       <?php 
           $query1="select * from purchase_details where invoice_no=$id";
           $query1_run=mysqli_query($connect , $query1);
           while($row=mysqli_fetch_array($query1_run)){
               $product_name = $row['product_name'];
           }
           echo $product_name[0];
       ?>
           <tr>
               <td><b id="number"></b></td>
               <td>
                   <select name="pid[]" class="form-control form-control-sm" required>
                       <option selected=""></option>
                   </select>
               </td>
               <td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>   
               <td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
               <td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
               <td></td>
           </tr>
        </tbody>
     </table>

标签: phpjquery

解决方案


您应该在表数据之后结束 while 循环

<table align="center" style="width:800px;">
      <thead>
             <tr>
                  <th>#</th>
                  <th style="text-align:center;">Item Name</th>
                  <th style="text-align:center;">Total Quantity</th>
                  <th style="text-align:center;">Quantity</th>
                  <th style="text-align:center;">Buy Price</th>
                  <th>Total</th>
             </tr>
       </thead>
       <tbody id="invoice_item">
       <?php 
           $query1="select * from purchase_details where invoice_no=$id";
           $query1_run=mysqli_query($connect , $query1);
           while($row=mysqli_fetch_array($query1_run)){
               $product_name = $row['product_name'];

           echo $product_name[0];
       ?>
           <tr>
               <td><b id="number"></b></td>
               <td>
                   <select name="pid[]" class="form-control form-control-sm" required>
                       <option selected=""></option>
                   </select>
               </td>
               <td><input name="tqty[]" readonly type="text" class="form-control form-control-sm" ></td>   
               <td><input name="qty[]" type="text" class="form-control form-control-sm" required value=""></td>
               <td><input name="price[]" type="text" class="form-control form-control-sm" readonly value=""></td>
               <td></td>
           </tr>
           <?php } ?>
        </tbody>
     </table>

推荐阅读