-1

Im new in php and sql right now so im confused whats wrong in my codes.(Need to do)cart_tbl (order_id) food_name,special_request, quantity, amount are equal to order_id of my order_tbl. When both order_id of my order_tbl and cart_tbl is same. My output will be the value of that 2 table. This is my code right now.

     <?php
$connect = mysqli_connect ("localhost", "root", "" , "db");
if(isset($_POST['order_id'])){ 
$asd = ($_POST['order_id']);
$sql = "SELECT food_name, special_request, quantity, amount 
FROM cart_tbl
WHERE order_id='$asd'";
$result = mysqli_query($connect, $sql);
}

?>

 <table class="table table-hover table-bordered">
 <thead>
 <tr> 
 <th>Food</th>
 <th>Special Request</th>
 <th>Quantity</th>
 <th>Amount</th> 
 </tr>
 </thead> 
 <?php
if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_array($result))
    {
?>
<tr>
<td><?php echo $row["food_name"];?></td>
<td><?php echo $row["special_request"];?></td>
<td><?php echo $row["quantity"];?></td>
<td><?php echo $row["amount"];?></td>
</tr>
<?php
    }
}
?>

</table>
Blank
  • 45
  • 1
  • 5

1 Answers1

0

Use INNER JOIN.

SELECT *
FROM cart_tbl
INNER JOIN order_tbl
ON cart_tbl.order_id = order_tbl.order_id
WHERE order_id='$asd'

Or, use NATURAL JOIN if the associated tables have the identical column name order_id and the columns are of same data type.

SELECT *
FROM cart_tbl
NATURAL JOIN order_tbl
WHERE order_id='$asd'
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I get the error "Undefined variable: result" I cant get the value of my textbox. Heres my code – Blank Sep 03 '17 at 06:45
  • @Blank *"Undefined variable: result" I cant get the value of my textbox.*, this is a very different issue than what you asked in the question. Make sure the `name` attributes are correct in your input elements. Also, do `var_dump($_POST);` and make sure the keys and the corresponding values are correct. BTW, in which line you're getting this *Undefined variable: result* error? – Rajdeep Paul Sep 03 '17 at 10:02
  • I used session but i cant get the value of the textbox of order_id. BTW its in if(mysqli_num_rows($result)>0) line i think. – Blank Sep 03 '17 at 17:36
  • @Blank 1. As I said, *Make sure the name attributes are correct in your input elements. Also, do var_dump($_POST); and make sure the keys and the corresponding values are correct.* Did you do that? 2. From where session came into picture? 3. Go through this SO thread to debug *undefined variable* issue, [https://stackoverflow.com/q/4261133/5517143](https://stackoverflow.com/q/4261133/5517143) – Rajdeep Paul Sep 03 '17 at 18:24