如何从下拉列表选择中检索数据到表?

时间:2022-08-24 16:54:38

I'm trying to learn how to program and this is my first project. I know i should read the tutorials but im kinda stuck with it. really wanting to accomplish it by myself, but it's getting frustrating.. can somebody at least guide me in the right direction? So, how do i echo the data to the table cells based on the drop down list selection? Any help much appreciated. code so far: http://jsfiddle.net/uFEPn/3/

我正在努力学习如何编程,这是我的第一个项目。我知道我应该阅读教程,但我有点坚持它。我真想自己完成它,但它变得令人沮丧..有人至少可以指导我朝着正确的方向前进吗?那么,如何根据下拉列表选择将数据回显到表格单元格?任何帮助非常感谢。代码到目前为止:http://jsfiddle.net/uFEPn/3/

<select>
    <option>-- Select lot --</option>
    <?php
    mysql_connect('localhost','root','');
    mysql_select_db('pl_base');
    $query="SELECT DISTINCT  lot_number FROM pl_table";
    $result=mysql_query($query);
    while(list($lot_number)=mysql_fetch_row($result)) {
        echo "<option value=\"".$lot_number."\">".$lot_number."</option>";
    }
   ?>
    </select>
</br>

<table border="1" id="table">
  <tr>
    <th width=80 height=30>Lot<br/>number</th>
    <th width=110 height=30>Description</th>
    <th width=90 height=30>Pallet<br/>number</th>
    <th width=60 height=30>Net</th>
    <th width=60 height=30>Gross</th>
  </tr>
  <tr>
    <td>#</td>
    <td rowspan="5">echo results here</td>
    <td><b>Total</b></td>
    <td>#</td>
    <td>#</td>
  </tr>
    <td> # </td>
    <td colspan="3">#</td>
  </tr>

  <tr>
    <th>&</th>
    <th>&</th>
    <th>&</th>
    <th>&</th>
  </tr>

  </table>

the table in data base:

数据库中的表:

+--------------------------+-------------------------+---------+-------+
|    id   |   lot_number   | descr  | pallet_number  | net     | gross |
+--------------------------+-------------------------+---------+-------+
|    1    |       111      |  black |         1      |  800    | 900   |
|    2    |       111      |  black |         2      |  801    | 901   |
|    3    |       111      |  black |         3      |  802    | 902   |
|    4    |       222      |  white |         1      |  800    | 900   |
|    5    |       222      |  white |         2      |  801    | 901   |
|    6    |       222      |  white |         3      |  802    | 902   |
+--------------------------+-------------------------+---------+-------+

1 个解决方案

#1


0  

Choosing the lot number in a select element will not update directly using php. since php is a server side language you would have to submit the data as a form and then display it.

选择元素中的批号不会直接使用php更新。由于php是服务器端语言,因此您必须将数据作为表单提交,然后再显示它。

    <?php 
     mysql_connect('localhost','root','');
        mysql_select_db('pl_base');
        $query="SELECT DISTINCT  lot_number FROM pl_table";
        $result=mysql_query($query);
     ?>  
     <form action="" method="POST">
     <select name="option_chosen">
        <option>-- Select lot --</option>
        <?php
            while(list($lot_number)=mysql_fetch_row($result)) {
            echo "<option value=\"".$lot_number."\">".$lot_number."</option>";
        }
       ?>
        </select>

     <input type="submit" value="Submit" />
    </form>
    </br>

    <table border="1" id="table">
      <tr>
        <th width=80 height=30>Lot<br/>number</th>
        <th width=110 height=30>Description</th>
        <th width=90 height=30>Pallet<br/>number</th>
        <th width=60 height=30>Net</th>
        <th width=60 height=30>Gross</th>
      </tr>

      <?php 
      if($_SERVER['REQUEST_METHOD'] =='POST')
 { $option_chosen=$_POST['option_chosen'];
     $query="SELECT * FROM pl_table WHERE lot_number='$option_chosen'";
     $run=mysqli_query($query);
     $row=mysqli_fetch_array($run, MYSQLI_ASSOC);

    echo "<tr><td>".$row['lot_number']."</td>";
    echo  "<td>".$row['descr']."</td><td>".$row['pallet_number']."</td>";
    echo "<td>".$row['net']."</td><td>".$['gross']."</td></tr>";

}
?>

         </table>

#1


0  

Choosing the lot number in a select element will not update directly using php. since php is a server side language you would have to submit the data as a form and then display it.

选择元素中的批号不会直接使用php更新。由于php是服务器端语言,因此您必须将数据作为表单提交,然后再显示它。

    <?php 
     mysql_connect('localhost','root','');
        mysql_select_db('pl_base');
        $query="SELECT DISTINCT  lot_number FROM pl_table";
        $result=mysql_query($query);
     ?>  
     <form action="" method="POST">
     <select name="option_chosen">
        <option>-- Select lot --</option>
        <?php
            while(list($lot_number)=mysql_fetch_row($result)) {
            echo "<option value=\"".$lot_number."\">".$lot_number."</option>";
        }
       ?>
        </select>

     <input type="submit" value="Submit" />
    </form>
    </br>

    <table border="1" id="table">
      <tr>
        <th width=80 height=30>Lot<br/>number</th>
        <th width=110 height=30>Description</th>
        <th width=90 height=30>Pallet<br/>number</th>
        <th width=60 height=30>Net</th>
        <th width=60 height=30>Gross</th>
      </tr>

      <?php 
      if($_SERVER['REQUEST_METHOD'] =='POST')
 { $option_chosen=$_POST['option_chosen'];
     $query="SELECT * FROM pl_table WHERE lot_number='$option_chosen'";
     $run=mysqli_query($query);
     $row=mysqli_fetch_array($run, MYSQLI_ASSOC);

    echo "<tr><td>".$row['lot_number']."</td>";
    echo  "<td>".$row['descr']."</td><td>".$row['pallet_number']."</td>";
    echo "<td>".$row['net']."</td><td>".$['gross']."</td></tr>";

}
?>

         </table>