添加产品不添加到产品代码


Adding Products Not Adding to Product Code

我一直在 stackoverflow 上寻找这个答案,但似乎找不到与此相关的答案,这真的让我很难想出解决这个问题的方法。

这个购物车的想法是,每个 productid(来自数据库)都是会话中数组的键 id(我想尽量保持相关性,如果所有 productid 都是唯一的,那就没问题了),如果它在那里找不到 productid,它会添加它们,或者在找到相同 productid 的那一行上,它会添加一个。

这是逻辑:

$product_id = (int) $_POST['product_id'];
$qty = (is_numeric($_POST['qty'])) ? (int) $_POST['qty'] : 1;
$sql = "SELECT * FROM products WHERE id = $product_id";
$result = mysql_query($sql) or die (mysql_error());
if($result)
{
    $row = mysql_fetch_row($result);
    $product_id = (int) $row[0];
    $product_array = array('product_id' => $product_id,
                              'product_code' => $row[1],
                              'product_name' => $row[2],
                              'image' => $row[5],
                              'qty' => $qty,
                              'price' => $row[6]);
    // check if we've already got a cart key in the session array:
    if(isset($_SESSION['cart']))
    {
        // loops through the items to find the row of the product being added to the already there cart session:
        foreach($_SESSION['cart'] as $k => $v)
        {
            // if the cart key which is based on the product id, is in the array, dont add it again
            if(in_array($k, $product_array))
            {
                // but add the desired qty to it:
                $_SESSION['cart'][$k]['qty'] += $qty;
            } else {
                // otherwise if it's not in the array, then add it like the one when we created the cart session key:
                $_SESSION['cart'][$product_array['product_id']] = $product_array;
            }
        }
    } else {
        // if not make one:
        $_SESSION['cart'][$product_array['product_id']] = $product_array;
    }
}

我遇到的问题是,如果我说去将产品 1 ID 1 添加到购物车,当我去添加产品 2 时(添加 ID 为 1 的产品 1 之后),它似乎在以前的产品上增加了一个数量,并且没有向购物车添加任何东西,而是另一个以前的产品。

我只是不明白为什么会发生这种情况,任何回复都非常感谢。

杰里米。

在我回答你的问题之前,我必须提到互联网安全的重要性,因此你应该将任何数据库访问代码切换到PHP的DBO库。

NetTuts在这里,这里和这里有一些非常好的教程。 除了使您的代码更安全之外,它还会使它更容易,我将对此进行演示。

我相信您遇到的具体问题是这条线in_array($k, $product_array). 它也没有像您期望的那样比较数组。 值得庆幸的是,我们可以使用$product_id来访问购物车中的数据。

随着PDO的更改和使用$product_id访问购物车,您的代码将变得更像这样:

$product_id = (int) $_POST['product_id'];
$qty = (is_numeric($_POST['qty'])) ? (int) $_POST['qty'] : 1;
try {
  // create db connection and make sure db/sql errors raise exceptions
  $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ;
  // query the database
  $query = $db->prepare("SELECT * FROM products WHERE id=:product_id");
  $query->execute(array('product_id' => $product_id));
  // fetch the first result as an associative array
  $product_array = $query->fetch();
  if ($product_array) {
    $product_id = $product_array['product_id'];
    $product_array['qty'] = $qty;
    // check if we've already got a cart key in the session array:
    if (isset($_SESSION['cart']) && isset($_SESSION['cart'][$product_id])) {
      $_SESSION['cart'][$product_id]['qty'] += $qty;
    }
    // if not make one
    else {
      $_SESSION['cart'][$product_id] = $product_array;
    }
  }
}
catch(PDOException $e) {
    echo $e->getMessage();
}