PHP - Mysqli - 错误更新购物车 [英] PHP - Mysqli - Error update cart shopping

查看:157
本文介绍了PHP - Mysqli - 错误更新购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i.stack.imgur.com/tBWVO.pngrel =nofollow>如图所示



问题是我必须刷新页面才能更新添加的产品。



此功能将产品的商品添加到购物车中。

  ------------------- 
(3) - >产品|
-------------------
数量:3 |
-------------------

<?php
if(isset($ _ SESSION [QuantityTotal] ))
echo $ _SESSION [QuantityTotal];
else {
echo0;
}
?>

此功能显示加入购物车的产品的总成本

  ------------------------------- 
(3) - >产品 - > $ 300.00 |
-------------------------------
总计:$ 300.00 |
-------------------------------

<?php
if(isset($ _ SESSION [totalcoste]))
echo $ _SESSION [totalcoste];
else {
echoCart empty;
}
?>

这些功能将显示在所有页面上以显示访问者。

  ------------------------------- 
(3) - >产品 - > $ 300.00 |
-------------------------------
数量:3合计:$ 300.00 |
-------------------------------

$ b $ p



$ p $ <?php
if(isset($ _GET ['id']))
$ id = $ _GET ['id'];
else
$ id = 1;
if(isset($ _ GET ['action']))
$ action = $ _GET ['action'];
else
$ action =empty;
switch($ action){
caseadd:
if(isset($ _ SESSION ['cart'] [$ id]))
$ _SESSION ['cart' ] [$ ID] ++;
else
$ _SESSION ['cart'] [$ id] = 1;
休息;
caseremove:
if(isset($ _ SESSION ['cart'] [$ id]))
{
$ _SESSION ['cart'] [$ id] - ;
if($ _ SESSION ['cart'] [$ id] == 0)
unset($ _ SESSION ['cart'] [$ id]);
}
break;
caseremoveProd:
if(isset($ _ SESSION ['cart'] [$ id])){
unset($ _ SESSION ['cart'] [$ id]);
}
break;
casemostrar:
if(isset($ _ SESSION ['cart'] [$ id])){
continue;
}
break;
caseempty:
unset($ _ SESSION ['cart']);
休息;
}
if(isset($ _ SESSION ['cart'])){
echo'< table id =table-card>';
$ totalcoste = 0;
$ xTotal = 0;

echo'< tr>
< td colspan =2id =td-card>名称产品< / td>
< td id =td-card-precio>价格< / td>
< td id =td-card-cant>数量< / td>
< td colspan =3id =td-card-total>小计< / td>
< / tr>';

echo'< tr>
< td colspan =7>< / td>
< / tr>';

foreach($ _ SESSION ['cart'] as $ id => $ x){
$ sql = mysqli_query($ con,SELECT * FROM products WHERE id = $ id );
$ mifila = mysqli_fetch_array($ sql);
$ id = $ mifila ['id'];
$ img = $ mifila ['img'];
$ price = $ mifila ['price'];
$ product = $ mifila ['name'];
$ product = substr($ product,0,40);
$ price = $ mifila ['price'];
$ coste = $ price * $ x;
$ totalcoste = $ totalcoste + $ coste;
$ xTotal = $ xTotal + $ x;

echo'< tr>
< td id =td-card-img>< img src =products /..$ img。'width =70height =120/>< / td> ;
< td id =td-card-name>'。 $ product。'< / td>
< td id =td-card-store> $'。$ price。'< / td>
< td colspan =2id =td-card-border>

< table id =table-card-edition>
< tr>< td>& nbsp;< / td>
< td>'。$ x。'< / td>
< td>';
echo< form action ='cart.php?id =。 $ id。& action = add'method ='post'>
< input class ='img'type ='image'src ='img / add.png'title ='Update'/> ;
< / form>
;
echo'< / td>< td>'; echo
< form action ='cart.php?id =。 $ id。& action = remove'method ='post'>
< input class ='img'type ='image'src ='img / remove.png'title ='subtract'/> ;
< / form>
;
echo'< / td>< td>';
echo< form action ='cart.php?id =。 $ id。& action = removeProd'method ='post'>
< input class ='img'type ='image'src ='img / delete.gif'title ='删除'/> ;
< / form>
;
echo'< / td>< / tr>< / table> < / TD>
< td id =td-card-subtotal> $'。$ coste。'< / td>
< td>& nbsp;< / td>
< / tr>';
}
echo'< tr>< td colspan =7>< / td>< / tr>
< tr>
< td colspan =5id =td-card-total-pay>总计< / td>
< td id =td-card-subtotal-pay> $'。$ totalcoste。'< / td>
< td>& nbsp;< / td>
< / tr>

< tr>
< td colspan =7>
< a href =shopping.php>< input id =buttontype =buttonvalue =Checkout/>< / a>< / td>
< / tr>
< / table>';
}
else
echo'篮子是空的';

$ _SESSION [totalcoste] = $ totalcoste;
$ _SESSION [quantityTotal] = $ xTotal;
echo'< br />< div class =volver> Volver a la< a href =product.php> products< / a>< / div>';

?>

购买产品的按钮就是这个功能

 < a href ='cart.php?id = $ id& action = add'><按钮>添加到购物车< /按钮>< / a> 

它等于这个函数

  cart.php?id = 3& action = add 

如图



帮助我修改代码,使其工作无需刷新页面

谢谢。

解决方案

输入此代码修复问题朋友

  if(isset($ _ SESSION ['cart' ]))
if(isset($ _ SESSION ['cart'])){
echo'< table id =table-card>';
$ totalcoste = 0;
$ xTotal = 0;

问候


Help please.

Error update cart shopping.

as pictured

The problem is that I have to refresh the page to update added products.

This function has the items of products added to cart.

-------------------
 (3) --> Product  |
-------------------
 Quantity: 3      |
-------------------

<?php 
     if(isset($_SESSION["QuantityTotal"]))
       echo $_SESSION["QuantityTotal"];
         else{
          echo "0";
       }
?>

This function shows the total cost of the products added to cart

-------------------------------
 (3) --> Product --> $300.00  |
-------------------------------
 Total: $300.00               |
-------------------------------

<?php 
    if(isset($_SESSION["totalcoste"]))
     echo $_SESSION["totalcoste"];
      else{
       echo "Cart empty";
     }
?>

These functions will be displayed on all pages to display the visitors.

-------------------------------
 (3) --> Product --> $300.00  |
-------------------------------
 Quantity: 3 Total: $300.00   |
-------------------------------

This is code complete cart shopping.

<?php
if (isset($_GET['id']))
$id = $_GET['id'];
else
$id = 1;
if (isset($_GET['action']))
$action = $_GET['action'];
else
$action = "empty";
switch($action){
case "add":
if(isset($_SESSION['cart'][$id]))
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if(isset($_SESSION['cart'][$id]))
    {
        $_SESSION['cart'][$id]--;
        if($_SESSION['cart'][$id]==0)
        unset($_SESSION['cart'][$id]);
    }
    break;
    case "removeProd":
    if(isset($_SESSION['cart'][$id])){
        unset($_SESSION['cart'][$id]);
    }
    break;
    case "mostrar":
    if(isset($_SESSION['cart'][$id])){
        continue;
    }
    break;
    case "empty":
    unset($_SESSION['cart']);
    break;
    }
    if(isset($_SESSION['cart'])){
        echo'<table id="table-card">';
        $totalcoste = 0;
        $xTotal = 0;

        echo'<tr>
                <td colspan="2" id="td-card">Name product</td>
                <td id="td-card-precio">Price</td>
                <td id="td-card-cant">Quantity</td>
                <td colspan="3" id="td-card-total">Subtotal</td>
                </tr>';

        echo '<tr>
                <td colspan="7"></td>
                </tr>';

        foreach($_SESSION['cart'] as $id => $x){
            $sql = mysqli_query($con,"SELECT * FROM products WHERE id=$id");
            $mifila = mysqli_fetch_array ($sql);
            $id = $mifila['id'];
            $img = $mifila['img'];
            $price = $mifila['price'];
            $product = $mifila['name'];
            $product = substr($product,0,40);
            $price = $mifila['price'];
            $coste = $price * $x;
            $totalcoste = $totalcoste + $coste;
            $xTotal = $xTotal + $x;

            echo '<tr>
                    <td id="td-card-img"><img src="products/'.$img.'" width="70" height="120" /></td>
                    <td id="td-card-name">'. $product .'</td>
                    <td id="td-card-store">$ '.$price.'</td>
                    <td colspan="2" id="td-card-border">

                    <table id="table-card-edition">
                    <tr><td>&nbsp;</td>
                      <td>'.$x.'</td>
                      <td>';
            echo"<form action='cart.php?id=". $id ."&action=add' method='post'>
                    <input class='img' type='image' src='img/add.png' title='Update'/>
                    </form>
                    ";
            echo'</td><td>';echo"
                    <form action='cart.php?id=". $id ."&action=remove' method='post'>
                    <input class='img' type='image' src='img/remove.png' title='subtract'/>
                    </form>
                    ";
            echo'</td><td>';
            echo"<form action='cart.php?id=". $id ."&action=removeProd' method='post'>
                    <input class='img' type='image' src='img/delete.gif' title='Delete'/>
                    </form>
                    ";
            echo'</td></tr></table> </td>
                    <td id="td-card-subtotal">$ '.$coste .'</td>
                    <td>&nbsp;</td>
                    </tr>';
                }
            echo'<tr><td colspan="7"></td></tr>
                <tr>
                <td colspan="5" id="td-card-total-pay">Total</td>
                <td id="td-card-subtotal-pay">$ '.$totalcoste.'</td>
                <td>&nbsp;</td>
                </tr>

                <tr>
                <td colspan="7">
                <a href="shopping.php"><input id="button" type="button" value="Checkout" /></a></td>
                </tr>
                </table>';
            }
            else
                echo 'The basket is empty';

            $_SESSION["totalcoste"] = $totalcoste;
            $_SESSION["quantityTotal"] = $xTotal;
            echo '<br/><div class="volver">Volver a la <a href="product.php">products</a></div>';

?>

The button of buy product is this function

<a href='cart.php?id=$id&action=add'><button>Add to cart</button></a>

which it is equal to this function

cart.php?id=3&action=add

as pictured

Help me to modify the code, to make it work without having to refresh the page

Thank you.

解决方案

Enter this code to fix the problem friend

if(isset($_SESSION['cart']))
    if(isset($_SESSION['cart'])){
        echo'<table id="table-card">';
        $totalcoste = 0;
        $xTotal = 0;

Greetings

这篇关于PHP - Mysqli - 错误更新购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆