我现在是php和sql的新手,所以我很困惑我的代码中的错误。(需要做的)cart_tbl(order_id)food_name,special_request,quantity,amount等于我的order_tbl的order_id。当我的order_tbl和cart_tbl的order_id相同时。我的输出将是该2表的值。这是我现在的代码。
<?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>
答案 0 :(得分:0)
使用INNER JOIN
。
SELECT *
FROM cart_tbl
INNER JOIN order_tbl
ON cart_tbl.order_id = order_tbl.order_id
WHERE order_id='$asd'
或者,如果关联的表具有相同的列名NATURAL JOIN
并且列具有相同的数据类型,请使用order_id
。
SELECT *
FROM cart_tbl
NATURAL JOIN order_tbl
WHERE order_id='$asd'