gpt4 book ai didi

php - 使用 session 在 php 中将商品添加到购物车

转载 作者:行者123 更新时间:2023-12-01 04:38:25 27 4
gpt4 key购买 nike

How cat looks like, there are items added twice我正在尝试更新添加到购物车 session 中的数量,但下面的代码仅增加添加的第一个项目的数量两次,然后每当添加任何其他项目两次时执行合并,kidhelp

$('.add-to-cart-mt').on('click', function(e){
e.preventDefault();
var id =$(this).attr('id');
$('#add-to-cat-dialog').dialog({
autoOpen:false,
modal:true,
hide:"pluff",
show:"slide",
height:200,
open: function() { $(".ui-dialog-titlebar-close").hide(); },

buttons:{
"Add":function (){
$('#add-to-cat-dialog').dialog("close");
$.ajax({
url: 'add_to_cart.php',
data: { productId : id },

success: function (data) {
$('.top-cart-contain').empty();
$('.top-cart-contain').load("header_cart_summary.php");
},

error :function (data, textStatus, jqXHR) { }
});
},

"Cancel":function (){
$(this).dialog("close");
}
}
});

$('#add-to-cat-dialog').dialog("open");
});
session_start();
require './database.php';

if(!empty($_GET["productId"])) {
$id=$_GET["productId"];
$productById = $connection->query("SELECT * FROM products WHERE productId='$id';");
if($productById && (mysqli_num_rows($productById)>0)){
echo 'query ok';
$productDetail= mysqli_fetch_assoc($productById);
$itemArray = array(
$productDetail["productId"]=>array(
'name'=>$productDetail["productName"],
'id'=>$productDetail["productId"],
'quantity'=>1,
'price'=>$productDetail["productPrice"],
'image'=>$productDetail["productsImage1"]
)
);

if(!empty($_SESSION["cart_item"])) {
if(in_array($productDetail["productId"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productDetail["productId"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += 1;
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}

//set product count in the bag
if(!empty($_SESSION['cart_volume'])){
$_SESSION['cart_volume'] += 1;
} else {
$_SESSION['cart_volume'] = 1;
}

}else {echo ''. mysqli_error($connection);}

} else {
echo 'no item';
}
[enter image description here][1]<li class="item col-lg-4 col-md-4 col-sm-6 col-xs-6 ">
<div class="product-item">
<div class="item-inner">
<div class="product-thumb has-hover-img">
<figure> <a title="Ipsums Dolors Untra" href="single_product.html"> <img class="first-img" src="./productImages/<?php echo $item['productsImage1']; ?>" style="height: 250px;" alt=""> <img class="hover-img" src="../images/products/img05.jpg" alt=""> </a></figure>
<div class="pr-info-area animated animate2"><a href="quick_view.html" class="quick-view"><i class="fa fa-search"><span>Quick view</span></i></a> <a href="wishlist.html" class="wishlist"><i class="fa fa-heart"><span>Wishlist</span></i></a> <a href="compare.html" class="compare"><i class="fa fa-exchange"><span>Compare</span></i></a> </div>
</div>
<div class="item-info">
<div class="info-inner">
<div class="item-title"> <h4><a title="Ipsums Dolors Untra" href="single_product.html"><?php echo $item['productName']; ?></a></h4> </div>
<div class="item-content">
<div class="rating"> <i class="fa fa-star-o"></i> <i class="fa fa-star-o"></i> <i class="fa fa-star-o"></i> <i class="fa fa-star-o"></i> <i class="fa fa-star-o"></i> </div>
<div class="item-price">
<div class="price-box">
<p class="special-price"> <span class="price-label">Special Price</span> <span class="price">Ksh. <?php echo number_format($item['productPrice'], 2) ; ?></span> </p>
<!--<p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price"> $567.00 </span> </p>-->
</div>
</div>
<div class="pro-action">
<button type="button" class="add-to-cart-mt" id="<?php echo $item['productId']; ?>"> <i class="fa fa-shopping-cart"></i><span> Add to Cart</span> </button>
</div>
</div>
</div>
</div>
</div>
</div>
</li>

最佳答案

看看PHP手册:PHP Manual - array_merge()

相关部分:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

您可能正在使用数字 ID。如果是,则此 ID 会转换为与您需要的 ID (productID) 不匹配的数字序列

$itemArray = array(
$productDetail["productId"]=>array(
'name'=>$productDetail["productName"],
'id'=>$productDetail["productId"],
'quantity'=>1,
'price'=>$productDetail["productPrice"],
'image'=>$productDetail["productsImage1"]
)
);

在本地查看此代码:

$a = array('6' => array('name'=>'john','id'=>'6','quantity'=>1));

$b = array('7' => array('name'=>'bill','id'=>'7','quantity'=>1));

$c = array('8' => array('name'=>'mike','id'=>'8','quantity'=>1));

$d = array('6' => array('name'=>'lucy','id'=>'6','quantity'=>1));

$_SESSION["cart_item"] = array_merge($a, $b, $c, $d);

var_dump($_SESSION["cart_item"]);
unset($_SESSION["cart_item"]);

$a = array('six' => array('name'=>'john','id'=>'6','quantity'=>1));

$b = array('seven' => array('name'=>'bill','id'=>'7','quantity'=>1));

$c = array('eight' => array('name'=>'mike','id'=>'8','quantity'=>1));

$d = array('six' => array('name'=>'lucy','id'=>'6','quantity'=>1));

$_SESSION["cart_item"] = array_merge($a, $b, $c, $d);

var_dump($_SESSION["cart_item"]);

我的结果是:

/am.php:13:
array(4) {
[0] =>
array(3) {
'name' =>
string(4) "john"
'id' =>
string(1) "6"
'quantity' =>
int(1)
}
[1] =>
array(3) {
'name' =>
string(4) "bill"
'id' =>
string(1) "7"
'quantity' =>
int(1)
}
[2] =>
array(3) {
'name' =>
string(4) "mike"
'id' =>
string(1) "8"
'quantity' =>
int(1)
}
[3] =>
array(3) {
'name' =>
string(4) "lucy"
'id' =>
string(1) "6"
'quantity' =>
int(1)
}
}
/am.php:26:
array(3) {
'six' =>
array(3) {
'name' =>
string(4) "lucy"
'id' =>
string(1) "6"
'quantity' =>
int(1)
}
'seven' =>
array(3) {
'name' =>
string(4) "bill"
'id' =>
string(1) "7"
'quantity' =>
int(1)
}
'eight' =>
array(3) {
'name' =>
string(4) "mike"
'id' =>
string(1) "8"
'quantity' =>
int(1)
}
}

这显示了 array_merge 如何处理数字索引(至少使用 PHP 7)。正如您所看到的,即使输入为字符串的数字索引也会像数字一样处理,从而丢失 ProductID 引用。

另一件事:你的代码容易受到 SQL 注入(inject)的攻击:

$id=$_GET["productId"];
$productById = $connection->query("SELECT * FROM products WHERE productId='$id';");

在这里阅读更多相关信息:PHP Manual - SQL Injection

关于php - 使用 session 在 php 中将商品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com