在数据库中添加其他数据时,为什么我的功能没有更新?

时间:2018-10-21 07:06:42

标签: php mysql

当我调用getCatItem()函数时,要在数据库中添加另一组数据时遇到问题。该功能仅允许在我的网站上显示一组数据,而无法添加另一组数据。我尝试使用另一种方法,但也无法使用。

这是我的初始代码:

function getCats(){

    global $con; 

    $get_cats = "select * from category";
    $run_cats = mysqli_query($con, $get_cats);

    while ($row_cats=mysqli_fetch_array($run_cats)){
        $cat_id = $row_cats['category_id']; 
        $cat_title = $row_cats['category'];
        echo "<ul><a href='Equipment.php?cat=$cat_id'>$cat_title</a></ul>";
    }

}

如果我不涉及“ cat”,则每次我向数据库添加数据时代码都会更新。

function getItem(){

    if(!isset($_GET['cat'])){

        global $con; 

        $get_pro = "select * from item order by RAND() LIMIT 0,30";
        $run_pro = mysqli_query($con, $get_pro); 

        while($row_pro=mysqli_fetch_array($run_pro)){
            $pro_id = $row_pro['itemID'];
            $pro_cat = $row_pro['cat_id'];
            $pro_title = $row_pro['item_name'];
            $pro_amount = $row_pro['item_amount'];
            $pro_detail = $row_pro['item_detail'];
            $pro_image = $row_pro['item_image'];

            echo "

        <figure>
        <img src='administrator/product_images/$pro_image' width ='150px' height ='300px'>
        <figcaption style ='text-align : center;'>Item Name : $pro_title</figcaption>
        <figcaption style ='text-align : center;'>Description : $pro_detail</figcaption>
        <figcaption style ='text-align : center;'>Quantity : $pro_amount</figcaption>
        <a class='button' href='Booking.php'>Book Now</a>
        </figure>

        ";

        }
    }
}

然后,我在代码中添加类别,该项目仅显示一次,因此无法再添加。

function getCatItem(){

    if(isset($_GET['cat'])){

        $cat_id = $_GET['cat'];

        global $con; 

        $get_cat_pro = "select * from item where cat_id='$cat_id'";
        $run_cat_pro = mysqli_query($con, $get_cat_pro); 
        $count_cats = mysqli_num_rows($run_cat_pro);

        if($count_cats==0){
            echo "<h2 style='padding:20px;'>No products where found in this category!</h2>";
        }

        while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){

            $pro_id = $row_cat_pro['item_id'];
            $pro_cat = $row_cat_pro['cat_id'];
            $pro_title = $row_cat_pro['item_name'];
            $pro_amount = $row_cat_pro['item_amount'];
            $pro_detail = $row_cat_pro['item_detail'];
            $pro_image = $row_cat_pro['item_image'];

            echo "
            <div id='columns' class='columns_4'>
             <figure>
        <img src='administrator/product_images/$pro_image' width ='150px' height ='300px'>
        <figcaption style ='text-align : center;'>Item Name : $pro_title</figcaption>
        <figcaption style ='text-align : center;'>Description : $pro_detail</figcaption>
        <figcaption style ='text-align : center;'>Quantity : $pro_amount</figcaption>
        <a class='button' href='Booking/Booking.php'>Book Now</a>
        </figure>
        </div>
        ";
        }

    }

}

1 个答案:

答案 0 :(得分:0)

更改getCats()功能,如下所示

function getCats(){

    global $con; 

    $get_cats = "select * from category";
    $run_cats = mysqli_query($con, $get_cats);

    $cat_row_array = array();

    while ($row_cats=mysqli_fetch_array($run_cats)){
        $cat_row_array = $row_cats
    }

     foreach ($cat_row_array as $row_c){ 
        $cat_id = $row_c['category_id']; 
        $cat_title = $row_c['category'];
        echo "<ul><a href='Equipment.php?cat=$cat_id'>$cat_title</a></ul>";
    }

}