简体   繁体   English

如何在PHP的表格中显示搜索结果

[英]How to display search results in a table in PHP

I am busy with a simple html/php script and all I want to do is display the results I search for from my database in a table form but nothing is showing. 我忙于一个简单的html / php脚本,我要做的就是以表格形式显示我从数据库中搜索到的结果,但是什么也没有显示。 Here is my code: 这是我的代码:

    <html>
    <head>
        <title>product sales for a specific customer</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </head>
    <body>
            <h3 align="center"> Please Search for a specific customer you want to see sales for: </h3>
            <br />
            <input type="text" name="txtNameSearch" />
            <input class="src_btn" type="submit" name="btnSearch" value="Search" />
                <table width="800" border="1" cellpadding="1" cellspacing="1">
                <tr>
                <th>Order Details Id</th>
                <th>Order ID</th>
                <th>Product Id</th>
                <th>Login ID</th>
                <th>Quantity</th>
                <th>Product Price per unit</th>
                <th>Product Name</th>
                <th>Product Descrp</th>
                <th>Genre</th>
                <th>Price</th>
                <th>Quantity Sold</th>
                </tr> 
<?php
if(isset($_POST["btnSearch"]))
{
$connection = mysqli_connect('localhost', 'root', '', 'bookstore');
$sql="SELECT * FROM order_details right join tblproduct on order_details.prod_id=tblproduct.prod_id WHERE id_login = $search";
$Joined_records=mysqli_query($connection,$sql);

if (isset($_POST['txtNameSearch'])){
    $search = $_POST['txtNameSearch'];
}
while ($row=mysqli_fetch_assoc($Joined_records)){
    echo"<tr>";
    echo"<td>".$row["order_details_id"]."</td>";
    echo"<td>".$row["order_id"]."</td>";
    echo"<td>".$row["prod_id"]."</td>";
    echo"<td>".$row["id_login"]."</td>";
    echo"<td>".$row["quantity"]."</td>";
    echo"<td>".$row["price_per_unit"]."</td>";
    echo"<td>".$row["prod_name"]."</td>";
    echo"<td>".$row["prod_descr"]."</td>";
    echo"<td>".$row["prod_cat"]."</td>";
    echo"<td>".$row["prod_price"]."</td>";
    echo"<td>".$row["prod_quan"]."</td>";
    echo"</tr>";

}
}

?>
    </body>
</html>

So what I want to do is for instance I type "3" in the search bar and I click submit I want all the values corresponding with that 3 in my database to display in a table form. 因此,例如,我想在搜索栏中键入“ 3”,然后单击“提交”,我希望数据库中与该3对应的所有值都以表格形式显示。

I see in your code: 我在您的代码中看到:

$sql="SELECT * 
  FROM order_details 
  right join tblproduct 
  on order_details.prod_id=tblproduct.prod_id 
  WHERE id_login = $search";

Leaving aside the problems with this SQL, you have not defined $search until later in the code. 撇开该SQL的问题,您直到代码后面都没有定义$search This needs to be done before you create your query string containing the embedded vault, and it must be properly escaped and must be quoted in the query: 这需要创建包含嵌入式保管库的查询字符串之前完成,并且必须对其进行正确的转义并在查询中用引号将其引起来:

$search = "'" . mysqli_real_escape_string(
       $connection, $_POST['txtNameSearch']
       ) . "'";

You should also add proper error checking and handling to (at least) the mysqli_query() call. 您还应该向(至少)mysqli_query()调用添加适当的错误检查和处理。

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

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