user8035610
user8035610

Reputation:

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

Upvotes: 0

Views: 2564

Answers (1)

symcbean
symcbean

Reputation: 48387

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

Upvotes: 1

Related Questions