while循环只返回1个产品行

时间:2017-08-13 21:14:54

标签: php mysql while-loop

晚上好,

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

但是我的产品编辑页面只显示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>";                        

                            } 

            }

&GT;

enter image description here

我很乐意帮助我展示所有类别的所有产品。

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

感谢。 斯坦。

1 个答案:

答案 0 :(得分:2)

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

<?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>";                        

                            } 

            }
?>