简体   繁体   English

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

[英]insert output from mysql query into html table one after another

heloo, i want to serarch particular row from mysql database and same should be displayed on html table. heloo,我想从mysql数据库中搜索特定的行,并且应该在html表上显示相同的行。 I have tried the below code but it only gives the single row which is result of current query, previous row get overwritten. 我试过下面的代码,但它只给出了当前查询的结果的单行,上一行被覆盖了。 I am fetching a row for particular id,next time when i give another id,another row should be fetched and it should be added to the table next to previous one.. 我正在获取特定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);


      ?> 

If you need to do this within a user session (one user has one list. Another got different) so you need to use session 如果您需要在用户会话中执行此操作(一个用户有一个列表,另一个用户则有所不同),那么您需要使用会话

In your example - 在您的示例中-

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

You need to use ajax and jquery to do this. 您需要使用ajax和jquery来执行此操作。

send request via ajax and get response as your row. 通过ajax发送请求并在您的行中获得响应。 Manipulate the table using jquery to append in the existing set of records 使用jquery处理表以追加到现有记录集中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM