我正在尝试添加第二个查询来从我调用orderdetail的db表中获取数据。当我这样做时,我从两个数据库表中得到结果,但我只得到我的订单表中的第一个条目以显示所有内容。
我从orderdetail表中提取的唯一数据是:
<td class="tdproduct"><?php echo $row2['ProductName']; ?> </td>
其他一切都是订单表,它只是重复相同的信息。任何人都知道为什么?
<?php
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$query2 = "SELECT * FROM orderdetail LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
$result2 = mysqli_query($con, $query2);
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Customer Email</th>
<th class="thproduct">Date</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if(isset($_POST['order_id']) && is_numeric($_POST['order_id'])) {
mysqli_query($con, "DELETE FROM orders WHERE order_id = ". $_POST['order_id'] ."")
or die("Could not DELETE: " . mysqli_error($con));
"The order was successfully deleted.";
} else {
Session::flash('order', 'The order was successfully deleted.');
}
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
if( $result2 ){
while($row2 = mysqli_fetch_assoc($result2)) :
?>
<form method="POST" action="orderhistory.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['email']; ?> </td>
<td class="tdproduct"><?php echo $row['date_ordered']; ?> </td>
<td class="tdproduct"><?php echo $row2['ProductName']; ?> </td>
<td class="tdproduct"><?php echo $row['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
endwhile;
}
?>
答案 0 :(得分:1)
<?php
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$query2 = "SELECT * FROM orderdetail LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
$result2 = mysqli_query($con, $query2);
$data = array();
$data2 = array();
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Customer Email</th>
<th class="thproduct">Date</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if(isset($_POST['order_id']) && is_numeric($_POST['order_id'])) {
mysqli_query($con, "DELETE FROM orders WHERE order_id = {$_POST['order_id']}")
or die("Could not DELETE: " . mysqli_error($con));
echo "The order was successfully deleted.";
} else {
Session::flash('order', 'The order was successfully deleted.');
}
if( $result ) :
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
if ($result2) {
while ($row2 = mysqli_fetch_assoc($result2)) {
$data2[] = $row2;
}
}
$i = 0;
foreach ($data as $key => $values) :
?>
<form method="POST" action="orderhistory.php">
<table>
<tr>
<td class="tdproduct"><?php echo $values['order_id']; ?> </td>
<td class="tdproduct"><?php echo $values['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $values['email']; ?> </td>
<td class="tdproduct"><?php echo $values['date_ordered']; ?> </td>
<td class="tdproduct"><?php echo $data2[$i]['ProductName']; ?> </td>
<td class="tdproduct"><?php echo $values['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $values['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $values['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</table>
</form>
<?php
$i++;
endforeach;
endif;
?>
重做了一些你的代码,但由于我无法测试它,我不确定它是否有效,但你可以尝试一下。
如您所见,这是一种不同的查询方式,因为我在循环数据之前将数据存储在数组中,我通常会这样做,因此更容易理解什么是&#39;如果稍后查看代码,则会继续。