简体   繁体   English

PHP-购物车删除项目?

[英]PHP-Shopping cart remove items?

I'm creating a shopping cart using PHP and PostgreSQL. 我正在使用PHP和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. 首先,为了XHTML,您应该关闭<input>标记。

<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. PHP的未unset基本上破坏了局部变量。 This will run once, destroy $result , then throw an E_NOTICE stating that $result is undefined. 这将运行一次,销毁$result ,然后抛出E_NOTICE,说明$result是未定义的。 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. 这将从您的项目字符串中的参考数字从CSGames表中删除。 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). 您需要维护一个cartID(如果用户未登录,则可以将其设置为会话ID,如果用户必须登录,则可以将其设置为用户ID)。

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)");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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