gpt4 book ai didi

php - while 循环仅返回 1 个产品行

转载 作者:行者123 更新时间:2023-11-29 06:55:53 25 4
gpt4 key购买 nike

大家晚上好

我正在开发的电子商务平台的后端有以下脚本设置。本质上,我想拉出产品,并拉出所有当前类别,以便可以将产品分配到特定类别。

但是我的产品编辑页面仅显示 1 个结果。

有趣的是,如果我删除与最后一个 SQL 查询相关的脚本,我的产品会显示,但没有类别选项。使用该脚本,只有 1 个产品显示正确的类别。

<?php
$myusername= $_SESSION['login_user'];

include '../ecommerce/connection.php';

$sql="SELECT * FROM products where status='yes' ";

// Posting Result
$result = mysqli_query($connection, $sql);

// Counting Results
$count=mysqli_num_rows($result);

if($count==0) {

echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";

} else {

if ($result = mysqli_query($connection, $sql)){

echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";


while($row = $result->fetch_array())

{

$product_no = $row['product_id'];

echo "<tr>";

echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";

echo "<td>" .$row['product_id'];
echo "</td> ";

echo "<td>" .$row['product_name'];
echo "</td> ";

echo "<td>" .$row['product_description'];
echo "</td> ";

echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";

echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";

echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";



echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";

echo "Parent Category <select name='category' style='height: auto;' >";

**include '../ecommerce/connection.php';

$sql="SELECT category_name FROM categories where status='yes' ";

// Posting Result
$result = mysqli_query($connection, $sql);

// Counting Results
$count=mysqli_num_rows($result);

if($count==0) {

echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";

} else {

if ($result = mysqli_query($connection, $sql)){

while($row = $result->fetch_array()) {

$category = $row['category_name'];

echo" <option value='$category' >".$category ."</option>";

}


}


}**


echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";

echo "</tr>";


}

echo "</table>";

}

}

?>

enter image description here

我希望获得有关如何展示所有类别的所有产品的任何帮助。

我已经尝试了我能想到的一切。

谢谢。斯坦.

最佳答案

您在产品和类别查询中使用了相同名称的变量。当类别位于产品循环内部时,您可以使用新查询重新分配该变量,这就是它崩溃的原因。更改第二个查询的变量名称,这应该可以修复您的脚本。

<?php
$myusername= $_SESSION['login_user'];

include '../ecommerce/connection.php';

$sql="SELECT * FROM products where status='yes' ";

// Posting Result
$result = mysqli_query($connection, $sql);

// Counting Results
$count=mysqli_num_rows($result);

if($count==0) {

echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";

} else {

if ($result = mysqli_query($connection, $sql)){

echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";


while($row = $result->fetch_array())

{

$product_no = $row['product_id'];

echo "<tr>";

echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";

echo "<td>" .$row['product_id'];
echo "</td> ";

echo "<td>" .$row['product_name'];
echo "</td> ";

echo "<td>" .$row['product_description'];
echo "</td> ";

echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";

echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";

echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";



echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";

echo "Parent Category <select name='category' style='height: auto;' >";

**include '../ecommerce/connection.php';

$sql2="SELECT category_name FROM categories where status='yes' ";

// Posting Result
$result2 = mysqli_query($connection, $sql2);

// Counting Results
$count=mysqli_num_rows($result2);

if($count==0) {

echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";

} else {

if ($result2 = mysqli_query($connection, $sql2)){

while($row2 = $result2->fetch_array()) {

$category = $row2['category_name'];

echo" <option value='$category' >".$category ."</option>";

}


}


}**


echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";

echo "</tr>";


}

echo "</table>";

}

}
?>

关于php - while 循环仅返回 1 个产品行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45664975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com