PHP购物车删除项目?

PHP购物车删除项目?,php,postgresql,Php,Postgresql,我正在使用PHP和PostgreSQL创建一个购物车。我已经设法使用存储在数组中的参考号将商品放入购物车。我试图通过允许用户单击复选框(就像我在将项目添加到购物车时所做的那样)然后删除项目来创建删除功能,但它所做的似乎只是刷新页面并删除表 到目前为止,我的代码是: <form action="shoppingcart.php" method="post" style="width: 80%"> <input type="submit" name="RemoveFromC

我正在使用PHP和PostgreSQL创建一个购物车。我已经设法使用存储在数组中的参考号将商品放入购物车。我试图通过允许用户单击复选框(就像我在将项目添加到购物车时所做的那样)然后删除项目来创建删除功能,但它所做的似乎只是刷新页面并删除表

到目前为止,我的代码是:

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


第一个恼怒:为了XHTML,你应该关闭你的
标签

<input type="submit" name="RemoveFromCart" value="Remove" />
PHP的
unset
基本上破坏了局部变量。这将运行一次,销毁
$result
,然后抛出一个E_通知,说明
$result
未定义。查看您是如何使用它的,您可能希望将查询更改为以下内容:

$result = pg_query($con, "DELETE FROM CSGames WHERE refnumber IN ($item_string)");
这将从CSGames表中删除项目字符串中的参考号。但是,如果多个用户正在使用此功能,则删除一个人的购物车项目可能会删除另一个人的购物车项目。您需要维护一个cartID(如果用户未登录,则可以将其设置为会话ID;如果用户必须登录,则可以将其设置为用户ID)

因此,您的目标如下:

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

$result = pg_query($con, "DELETE FROM CSGames WHERE refnumber IN ($item_string)");
<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)");
        }
    }