-1

image of the cart (sorry it's in Frensh)

I have already set the cart using sessions but now I'm updating the code to add it to the database, but I have an issue in updating when the quantity changes and I don't know where the problem is.

Adding and deleting work perfectly but when I increase the quantity it's always set to zero in the database and the total stay the same.

Here's the code of the three buttons : remove - add - delete

 <td><a class='btn btn-warning' href='../Ressources/cart.php?remove= 
{$row['product_id']}'><span class='glyphicon glyphicon-minus'></span></a>
    <a class='btn btn-success' href='../Ressources/cart.php?add= 
{$row['product_id']}'><span class='glyphicon glyphicon-plus'></span></a>
    <a class='btn btn-danger' href='../Ressources/cart.php?delete= 
{$row['product_id']}'><span class='glyphicon glyphicon-remove'></span></a>
</td>

and this is the PHP code :

/*****************************add**********************/
if (isset($_GET['add'])) {
    $id = $_SESSION['id_user'];
    $idp = $_GET['add'];
    $query = query("SELECT * FROM products WHERE product_id=".escape_string($_GET['add']). " ");
    confirm($query);

    while ($row = fetch_array($query)) {
        if ($row['product_quantity'] != $_SESSION['product_' . $_GET['add']]) {
            $_SESSION['product_' . $_GET['add']] += 1;
            $quantity = $_SESSION['product_' . $_GET['add']];
            $total = $quantity * $row['product_price'];
            $query2 = query("SELECT * FROM cart where id_product=$idp and id_user=$id");
            confirm($query2);
            if (mysqli_num_rows($query2) == 0) {
                $query1 = query("INSERT into cart values ($id,$idp,$quantity,$total)");
            } else {
                $query1 = query("UPDATE cart set quantity=$quantity and total=$total where id_product=$idp and id_user=$id");
            }
            confirm($query1);
            redirect("../Public/checkout.php");
        } else {
            set_message("there's only " . $row['product_quantity'] . " 
            " . "available");
            redirect("../Public/checkout.php");
        }
    }
}

/*************** remove *********************/
if (isset($_GET['remove'])) {
    $idp = $_GET['remove'];
    $id = $_SESSION['id_user'];
    $_SESSION['product_' . $_GET['remove']]--;
    $quantity = $_SESSION['product_' . $_GET['remove']];
    $total = $_SESSION['item_total'];
    $query = query("UPDATE cart set quantity=$quantity and total=$total 
        where id_product=$idp and id_user=$id");
    confirm($query);
    if ($_SESSION['product_' . $_GET['remove']] < 1) {
        unset($_SESSION['item_total']);
        unset($_SESSION['item_quantity']);
        unset($_SESSION['nb_product']);
        $query1 = query("DELETE FROM cart where id_product=$idp and id_user=$id");
        confirm($query1);
        redirect("../Public/checkout.php");
    } else {
        redirect("../Public/checkout.php");
    }
}

/*************** delete ********************/
if (isset($_GET['delete'])) {
    $idp = $_GET['delete'];
    $id = $_SESSION['id_user'];
    $_SESSION['product_' . $_GET['delete']] = '0';
    unset($_SESSION['item_total']);
    unset($_SESSION['item_quantity']);
    unset($_SESSION['nb_produit']);
    $query1 = query("DELETE FROM cart where id_product=$idp and id_user=$id");
    confirm($query1);
    redirect("../Public/checkout.php");
}

There's still a function in the code to display the products in the cart ( in a table) but I need to solve this problem first)
I expect the values of the quantity and total to be stored in the database as the sessions, any help would be appreciated

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

-1

i had an error in the syntax of the update query, it works now