L. Dan L. Dan - 5 months ago 124
SQL Question

PHP - Mysqli - Error update cart shopping

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.

Answer

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