に私のコードを改訂しました機能商品が既にカートに入っていれば、数量を更新しないといくつかの問題があります。 。 カートに追加された最初の製品のみが正しく更新されます。は、カートのコードに追加UPDATEDショッピングカート - 数量カートの最初の項目のみを更新します。
私は次のとおりです。私は、コードで何かを
// -------------------------
// Adding item
// -------------------------
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
$itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["product_id"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
足りませんか?
ありがとうございました。 スタン。
編集: 更新されたコードのdoesntの仕事:
// -------------------------
// Adding item
// -------------------------
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
$itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["product_id"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
**foreach($_SESSION["cart_item"] as $k =>&$v){
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}**
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃の脆弱性があります。あなたは[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)ドライバ。 [**この記事**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)には、いくつかの良い例があります。 –
ライブURLとデータベース構造に関する情報を投稿したので、すぐにオフラインにすることをお勧めします*。ネット上の誰でもあなたのデータベースを抹消することができます。 –
ここでデモにリンクするのは最良の方法ではありません。 [SOの最小限の検証可能なサンプルを作成する方法](https://stackoverflow.com/help/mcve)をご覧ください。フォームのhtmlを含めてください。 –