将mysql查询的输出一个接一个地插入到html表中

时间:2013-12-30 10:23:03

标签: php html mysql

heloo,我想从mysql数据库中分离出特定的行,同样应该在html表上显示。我已经尝试了下面的代码,但它只给出了当前查询结果的单行,前一行被覆盖。我正在获取特定id的行,下次当我给另一个id时,应该获取另一行并且应该将其添加到前一个旁边的表中。

     <html>
    <body>
    <form action="<?php $_PHP_SELF ?>" method="POST">
    <tr>
    ProductID: <input type="number" name="ProductID">
    <input type="submit" value ="Go"> 
   </form>
   </tr>
 </body>
 </html>


 <?php

   $con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
 {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  mysqli_select_db("Inventory",$con);
 $ID=$_POST['ProductID'];
 //echo $ID;
 $result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm  Products_Sold where ProductID =" .$ID);

echo"<table border = '1'>
<tr>
<th>ProductID</th>
<th>ProductName</th>
<th>UnitPrice</th>
</tr> ";
//$row=mysqli_fetch_array($result);
 //$c1= $row['ProductID'];
 //$c2=$row['ProductName'];
  //$c3=$row['UnitPrice'];
  //echo $c1; 
 //echo $c2;
 //echo $c3;

    //$ins= mysqli_query($con,"insert into Temp (ProductID,ProductName,UnitPrice) values ('%d','%s','%f')", $c1,$c2,$c3);

   //$fetch=mysqli_query($con,"select ProductId,ProductName,UnitPrice from  Temp");
      while( $row = mysqli_fetch_array($result))
     { echo "<tr>";
     echo "<td>". $row['ProductID'] . "</td>";
     echo  "<td>" . $row['ProductName'] ." </td>";
     echo   "<td>" . $row['UnitPrice'] . "</td>";
     echo "</tr> ";

     }
     echo "</table>";

      mysqli_close($con);


      ?> 

2 个答案:

答案 0 :(得分:0)

如果您需要在用户会话中执行此操作(一个用户有一个列表。另一个用户有不同),因此您需要使用session

在你的例子中 -

<?php
session_start();
   $con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  mysqli_select_db("Inventory",$con);
 $ID=$_POST['ProductID'];
 $_SESSION['ids'][mysql_escape_string($ID)] = array();
 //echo $ID;
 $result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm  Products_Sold where ProductID IN (\"" . implode('","', usort(array_keys( $_SESSION['ids']))). '");');
echo"<table border = '1'>
<tr>
    <th>
        ProductID
    </th>
    <th>
        ProductName
    </th>
    <th>
        UnitPrice
    </th>
</tr>
 "; 
while( $row = mysqli_fetch_array($result)) { echo "
<tr>
    "; echo "
    <td>
        ". $row['ProductID'] . "
    </td>
    "; echo "
    <td>
        " . $row['ProductName'] ."
    </td>
    "; echo "
    <td>
        " . $row['UnitPrice'] . "
    </td>
    "; echo "
</tr>
 "; } echo "
</table>
";
      mysqli_close($con);
      ?>

答案 1 :(得分:0)

您需要使用ajax和jquery来执行此操作。

通过ajax发送请求并获取响应作为您的行。使用jquery操作表以附加到现有记录集