简体   繁体   中英

PHP-Shopping cart remove items?

I'm creating a shopping cart using PHP and PostgreSQL. I've managed to get the items into the shopping cart using a reference number stored in an array. I'm trying to create the remove function by allowing the user to click on a check box (like I do in adding the item to the cart) and then removing the item, but all it seems to be doing is just refreshing the page and removing the table.

My code so far:

<form action="shoppingcart.php" method="post" style="width: 80%">
    <input type="submit" name="RemoveFromCart" value="Remove">

    <?php
    if(isset($_POST['itemsre']))
    {
        $n = count($_POST['itemsre']);
        for($i = 0; $i < $n; $i++)
        {

        }

        $items = array();
        foreach($_POST['itemsre'] as $item)
        {
            $items[] = pg_escape_string($con, $item);
        }

        if(!$_SESSION["deletingrows"])
        {

            $item_string = "'" . implode("','", $items) . "'";

            $result = pg_query($con, "SELECT title, platform, description, price FROM CSGames  WHERE refnumber IN ($item_string)");

            while($result)
            {
                unset($result);
            }
        }
    }

First pet peeve: you should close your <input> tag, just for XHTML's sake.

<input type="submit" name="RemoveFromCart" value="Remove" />

I'm not sure what this is used for:

$n = count($_POST['itemsre']);
for($i = 0; $i < $n; $i++)
{

}

It appears to be unused in your file. Again, it shouldn't affect the problem too much, but it adds code where there really doesn't need to be code.

I think the problem, ultimately, lies with this:

while($result)
{
    unset($result);
}

PHP's unset basically destroys the local variable. This will run once, destroy $result , then throw an E_NOTICE stating that $result is undefined. Looking at how you're using it, you may want to change your query to something like this:

$result = pg_query($con, "DELETE FROM CSGames WHERE refnumber IN ($item_string)");

This will delete from your CSGames table where the reference number is in your items string. However, if multiple users are using this, deleting one person's cart items may delete another's cart items. You need to maintain a cartID (which can be set to either the session ID if users don't log in, or the user ID if users must log in).

Therefore, your goal would be something as follows:

<form action="shoppingcart.php" method="post" style="width: 80%">
    <input type="submit" name="RemoveFromCart" value="Remove" />

    <?php
    if(isset($_POST['itemsre']))
    {

        $items = array();
        foreach($_POST['itemsre'] as $item)
        {
            $items[] = pg_escape_string($con, $item);
        }

        if(!$_SESSION["deletingrows"])
        {

            $item_string = "'" . implode("','", $items) . "'";
            $cartID = $_SESSION['userID']; // This must be changed to how you maintain unique users!
            $result = pg_query($con, "DELETE FROM CSGames WHERE cartID=$cartID AND refnumber IN ($item_string)");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM