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

Adding Products Not Adding to Product Code

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

这个购物车的想法是每个 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;
    }
}

我遇到的问题是,如果我说去将 ID 为 1 的产品 1 添加到购物车,当我去添加产品 2(在添加 ID 为 1 的产品 1 之后)时,它似乎又添加了一个数量添加到之前的产品,但未将任何内容添加到购物车,而是添加了另一个之前的产品。

我只是不明白为什么会这样,非常感谢任何回复。

杰里米

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

NetTuts here and here and here 提供了一些非常好的教程。除了使您的代码更安全之外,它还会使它更容易,我将演示这一点。

我认为您遇到的具体问题是这一行 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();
}