如何在网页上检索时从数据库中追加数据?

时间:2017-03-27 20:48:14

标签: javascript php html css mysql

每当我在文本框中输入ID号并单击扫描按钮时,我从数据库中获取数据,它以预定义的格式显示在文本框下方。现在,当我输入另一个id并点击扫描时,这样检索的新数据将替换旧数据。我希望它显示在页面上现有数据的下方。

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
                <meta charset="utf-8" />
        <title> ShopNGo </title>
                <link rel="stylesheet" type="text/css" href="style.css">

        </head>
    <body>
                <header id="header">
                        <div class="container">
                                <form name="products" method="POST">
                                        <br><br>
                            <button type="submit" name="scan" id="scan"> <h1> SCAN! </h1> </button>
                                        <br><br><br>
                                        <input type="text" name="id">
                        </form>
                        </div>
                </header>
                <div class="main">
            <table border="0">
<?php
                      if (isset($_POST["scan"])) {

                    $servername = "localhost";
                    $username = "#";
                    $password = "#";
                    $dbname = "#";

                    $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection Failed:" . mysqli_connect_error());
                                $query = "SELECT name, price, img FROM product WHERE id = $_POST[id]";
                                $result = mysqli_query($conn, $query);

                    if(mysqli_num_rows($result) > 0)
                    {
                                            while($row = mysqli_fetch_assoc($result))
                                            {
                        echo "<tr> <table border='0'> <tr>";
                        echo "<img src='$row[img]'>";
                        echo "<br>";
                        echo $row["name"];
                        echo "<br>";
                        echo $row["price"];
                        echo "</tr> </table> </tr>";
                                            }
                    }
                                    mysqli_close($conn); }
            ?> 
                </table>
            </div>
        </body>
</html>

请帮助我!!

如果你能提出现有代码的一些改进,除了我的要求之外,这将是一个很大的帮助......非常感谢你!

1 个答案:

答案 0 :(得分:0)

使用ajax将你的id发布到另一个检查数据id的php脚本,将html放入一个字符串变量中,然后在最后回显它。您可以使用ajax成功回调来附加数据

     $.ajax({
type: "POST",
  url: url,
data: id,
success: function(data){
   $('#targetDiv').append(data);
     }
});

php脚本:

    $query = "SELECT name, price, img FROM product WHERE id = $_POST[id]";
                            $result = mysqli_query($conn, $query);

                if(mysqli_num_rows($result) > 0)
                {
                                        while($row = 
    mysqli_fetch_assoc($result))
                                        {
                    $element = "<tr> <table border='0'> <tr>";
                    $element .= "<img src='$row[img]'>";
                     $element .= "<br>";
                     $element .=$row["name"];
                     $element .= "<br>";
                     $element .= $row["price"];
                     $element .= "</tr> </table> </tr>";
                                        }
                }


          echo $element;