第二个查询只导致它的数据出现在 while 循环中
Second query causing only data from it to appear in a while loop
我正在尝试添加第二个查询以从数据库中获取数据 table 我调用了 orderdetail。当我这样做时,我从两个数据库 table 得到结果,但我只得到订单中的第一个条目 table 以显示所有内容。
我从 orderdetail table 中提取的唯一一条数据是:
<td class="tdproduct"><?php echo $row2['ProductName']; ?> </td>
其他都是订单table,它只是重复相同的信息。任何人都知道为什么?
<?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;
}
?>
<?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;
?>
修改了你的一些代码,但由于我无法测试它,我不确定它是否有效,但你可以尝试一下。
如您所见,这是一种不同的查询方式,因为我在循环遍历数据之前将数据存储在数组中,所以我通常这样做,以便稍后查看代码时更容易理解发生了什么.
我正在尝试添加第二个查询以从数据库中获取数据 table 我调用了 orderdetail。当我这样做时,我从两个数据库 table 得到结果,但我只得到订单中的第一个条目 table 以显示所有内容。
我从 orderdetail table 中提取的唯一一条数据是:
<td class="tdproduct"><?php echo $row2['ProductName']; ?> </td>
其他都是订单table,它只是重复相同的信息。任何人都知道为什么?
<?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;
}
?>
<?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;
?>
修改了你的一些代码,但由于我无法测试它,我不确定它是否有效,但你可以尝试一下。
如您所见,这是一种不同的查询方式,因为我在循环遍历数据之前将数据存储在数组中,所以我通常这样做,以便稍后查看代码时更容易理解发生了什么.