简体   繁体   中英

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. 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..

     <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.

send request via ajax and get response as your row. Manipulate the table using jquery to append in the existing set of records

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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