私は多次元配列を製品に持っています。ユーザーが数量を変更したときに新しいものを追加するのではなく、既存のものを更新したいと思います。私はマージしようとしていますが、何らかの理由で多次元配列の名前が変わってしまい、なぜそれが見えません。マージの代わりに多次元に配列を追加
それは以下のようになります。 https://i.imgur.com/zEZVDXX.png
if(!empty($_POST["addquantity"])) {
$add_id = $_GET['add'];
$add_quantity = $_POST['addquantity'];
$sqlOrd = "SELECT * FROM products WHERE id_product = '$add_id'";
$result = mysqli_query($conn, $sqlOrd);
while($row=mysqli_fetch_assoc($result)) {
$productByCode[] = $row;
}
$itemArray = array(
$productByCode[0]["id_product"] => array(
'name' => $productByCode[0]["name"],
'id_product' => $productByCode[0]["id_product"],
'quantity' => $_POST["addquantity"],
'price' => $productByCode[0]["price"]
)
);
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["id_product"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["id_product"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["addquantity"];
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
http://idownvotedbecau.se/imageofcode – theFunkyEngineer
@theFunkyEngineerコードのない画像がありません - VARSコンテンツの画像には、いくつかの説明を含む、あり..あなたが文句を言うことができる唯一の事はそれということであり、インラインではありません。 – Philipp
コードを分割する必要があります。あなたは1か所であまりにも多くのことをやっています。また、SQLインジェクションに対しても非常に脆弱です。 –