Php 购物车数据未显示在html表中

Php 购物车数据未显示在html表中,php,html,mysql,Php,Html,Mysql,我制作了一个简单的购物车,我想在其中显示购物车中的选定项目。当我print\rmyarray显示数据时,我可以成功地选择项目。甚至session['cart']的计数也会增加。但不幸的是,它没有在我的边栏表中显示数据。这是我的密码。请给我任何建议。我坚持这个。 代码 <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['i

我制作了一个简单的购物车,我想在其中显示购物车中的选定项目。当我
print\r
myarray显示数据时,我可以成功地选择项目。甚至
session['cart']
的计数也会增加。但不幸的是,它没有在我的边栏表中显示数据。这是我的密码。请给我任何建议。我坚持这个。

代码

     <?php

        if(isset($_GET['action']) && $_GET['action']=="add"){
           $id=intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
    //$index=$id.$color.$size;
$sql_p="SELECT * FROM products WHERE productid={$id}";

$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
    $row_p=mysqli_fetch_array($query_p);
if(isset($_SESSION['cart'][$id])  && isset($_SESSION['cart'][$id]['color']) && $_SESSION['cart'][$id]['color'] == $color && isset($_SESSION['cart'][$id]['size']) && $_SESSION['cart'][$id]['size'] == $size)
        {
            $_SESSION['cart'][$id]['quantity']++;
           // $_SESSION['cart'][$index] = array('quantity' => $_SESSION['cart'][$id]['quantity'], 'price' => $row_p['product_price'], 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row_p['product_name']);

            print_r($_SESSION['cart'][$id]);
            echo "\n";
            //print_r($_SESSION['cart'][$index]);

            echo "<script>alert('".$row_p['product_name']." has been added to your cart.');</script>";
}
else{

   // $_SESSION['cart'][$index]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
     $_SESSION['cart'][$row_p['productid']]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
       print_r($_SESSION['cart'][$row_p['productid']]);
    }
}
}
?>
     <div id="sidebar">
            <h1>Cart:   <?php echo  count($_SESSION['cart']); ?></h1>
            <table border="1">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Quantity</th>
                    <th>color</th>
                    <th>size</th>
                    <th></th>
                </tr>
                <?php
                if(isset($_SESSION['cart'])){

                        $sql="Select * from products where productid IN (";
                        foreach($_SESSION['cart'] as $id => $value){
                            $sql .=$id. ",";
                        }

                        $sql=substr($sql,0,-1) . ") ORDER BY productid ASC";
                        $query = mysqli_query($con, $sql);
                        if(!empty($query)){
                            while($row = mysqli_fetch_array($query)){

                                ?>




                        <tr>
                            <td><?php echo $row['productid'] ?></td>
                            <td><?php echo $row['product_name']?></td>
                       <td><?php echo $_SESSION['cart'][$row['productid']]['quantity']; ?></td>
                            <td><?php echo  $_SESSION['cart'][$row['productid']]['color']?></td>
                            <td><?php echo $_SESSION['cart'][$row['productid']]['size'] ?></td>
                            <td><a href="index.php?page=remove_from_cart&action=remove&id=<?php echo $row['productid']; ?>">x</a> </td>
                        </tr>

                    <?php

                    }
                    }
                    ?>

          <tr><td colspan="3"><a href="index.php?page=cart">Go To Cart</a></td></tr> <tr><td><?php echo "Your Cart is Empty"; ?></td></tr>

        </table>
      </div>

购物车表

     <?php

        if(isset($_GET['action']) && $_GET['action']=="add"){
           $id=intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
    //$index=$id.$color.$size;
$sql_p="SELECT * FROM products WHERE productid={$id}";

$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
    $row_p=mysqli_fetch_array($query_p);
if(isset($_SESSION['cart'][$id])  && isset($_SESSION['cart'][$id]['color']) && $_SESSION['cart'][$id]['color'] == $color && isset($_SESSION['cart'][$id]['size']) && $_SESSION['cart'][$id]['size'] == $size)
        {
            $_SESSION['cart'][$id]['quantity']++;
           // $_SESSION['cart'][$index] = array('quantity' => $_SESSION['cart'][$id]['quantity'], 'price' => $row_p['product_price'], 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row_p['product_name']);

            print_r($_SESSION['cart'][$id]);
            echo "\n";
            //print_r($_SESSION['cart'][$index]);

            echo "<script>alert('".$row_p['product_name']." has been added to your cart.');</script>";
}
else{

   // $_SESSION['cart'][$index]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
     $_SESSION['cart'][$row_p['productid']]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
       print_r($_SESSION['cart'][$row_p['productid']]);
    }
}
}
?>
     <div id="sidebar">
            <h1>Cart:   <?php echo  count($_SESSION['cart']); ?></h1>
            <table border="1">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Quantity</th>
                    <th>color</th>
                    <th>size</th>
                    <th></th>
                </tr>
                <?php
                if(isset($_SESSION['cart'])){

                        $sql="Select * from products where productid IN (";
                        foreach($_SESSION['cart'] as $id => $value){
                            $sql .=$id. ",";
                        }

                        $sql=substr($sql,0,-1) . ") ORDER BY productid ASC";
                        $query = mysqli_query($con, $sql);
                        if(!empty($query)){
                            while($row = mysqli_fetch_array($query)){

                                ?>




                        <tr>
                            <td><?php echo $row['productid'] ?></td>
                            <td><?php echo $row['product_name']?></td>
                       <td><?php echo $_SESSION['cart'][$row['productid']]['quantity']; ?></td>
                            <td><?php echo  $_SESSION['cart'][$row['productid']]['color']?></td>
                            <td><?php echo $_SESSION['cart'][$row['productid']]['size'] ?></td>
                            <td><a href="index.php?page=remove_from_cart&action=remove&id=<?php echo $row['productid']; ?>">x</a> </td>
                        </tr>

                    <?php

                    }
                    }
                    ?>

          <tr><td colspan="3"><a href="index.php?page=cart">Go To Cart</a></td></tr> <tr><td><?php echo "Your Cart is Empty"; ?></td></tr>

        </table>
      </div>

购物车:
身份证件
名称
量
颜色
大小

@Steve你能用一些我能很容易理解的代码详细说明一下吗!一旦你开始工作,你需要让你的
$get['…
通过清理并转义变量,变量是安全的-真的很快。这篇文章底部的评论可能会帮助你:@Steve你编辑的那行代码对我不起作用。我再次编辑了我的代码。我在这里也在编辑。这段代码起作用了,但问题是当我用颜色
黄色
和大小选择
item1
时de>XL
。它在购物车表中显示数据。现在,如果我再次选择相同大小和颜色的同一商品,它只会更新表中的数量!!直到这里它工作正常!!但当我选择不同颜色和大小的同一商品时,它会用相同的id号替换旧表行。我想为此显示另一行。你能不能请帮助我。我不确定您使用的$id是否与$index相同-$index似乎包含大小和颜色标识符-我无法确定数据放回数据库的位置,因此它将作为
$行通过搜索找到[…
@Steve更新了我的代码。请看一看。我删除了$index。它的工作原理与我在上一篇文章中提到的一样comment@Steve你能用一些我能很容易理解的代码来详细说明一下吗?一旦你的
$get开始工作,你需要让你的
$get['…
通过清理并转义变量,变量是安全的-真的很快。这篇文章底部的评论可能会帮助你:@Steve你编辑的那行代码对我不起作用。我再次编辑了我的代码。我在这里也在编辑。这段代码起作用了,但问题是当我用颜色
黄色
和大小选择
item1
时de>XL
。它在购物车表中显示数据。现在,如果我再次选择相同大小和颜色的同一商品,它只会更新表中的数量!!直到这里它工作正常!!但当我选择不同颜色和大小的同一商品时,它会用相同的id号替换旧表行。我想为此显示另一行。你能不能请帮助我。我不确定您使用的$id是否与$index相同-$index似乎包含大小和颜色标识符-我无法确定数据放回数据库的位置,因此它将作为
$行通过搜索找到[…
@Steve更新了我的代码。请看一看。我删除了$index。它的工作原理与我在上一篇评论中提到的一样