Php 将mysql查询的输出依次插入html表

Php 将mysql查询的输出依次插入html表,php,html,mysql,Php,Html,Mysql,heloo,我想搜索mysql数据库中的特定行,同样的行应该显示在html表中。我已经尝试了下面的代码,但它只给出了当前查询的结果的单行,上一行被覆盖。我正在为特定id提取一行,下次我提供另一个id时,应提取另一行,并将其添加到上一行旁边的表中 <html> <body> <form action="<?php $_PHP_SELF ?>" method="POST"> <tr> Product

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);


      ?> 

如果需要在用户会话中执行此操作,则一个用户有一个列表。另一个不同,所以你需要使用

在你的例子中—

<?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);
      ?>

您需要使用ajax和jquery来实现这一点


通过ajax发送请求,并作为行获取响应。使用jquery操作表,将其追加到现有的记录集中

,使用PhP,您无法随心所欲。您需要提供一个要搜索的id列表,或者您需要考虑对另一个页面进行Ajax调用,以便在不删除其他行的情况下将其添加到表中。php在服务器端工作,因此它将在每个请求中覆盖您的记录。可以使用jquery和ajax追加下一行。