我希望用户在点击确认按钮后将所选项目从购物车插入数据库。问题是,在插入项目后,数量值不正确,因为我假设。我希望插入的数量由用户在购物车中确定。但事实是插入的数量是由管理员从数据库中确定的数量库存的值。
以下是查看购物车的代码:
<table>
<tr>
<th>Peralatan Sukan</th>
<th>Kuantiti</th>
</tr>
<?php
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM peralatansukan WHERE no IN(";
foreach((array) $_SESSION['cart'] as $id => $value){
$sql .=$id. ",";
}
$sql=substr($sql,0,-1) . ") ORDER BY no ASC";
$query = mysql_query($sql);
if(!empty($query)){
while($row = mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $row['peralatansukan']; ?></td>
<td><input type="text" name="kuantiti[<?php echo $row['no']; ?>]"
size="6" value="<?php echo $_SESSION['cart'][$row['no']]['kuantiti']; ?
>"> </td>
以下是更新购物车中数量的代码:
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['kuantiti']++;
}else{
$sql_p="SELECT * FROM peralatansukan WHERE no={$id}";
$query_p=mysql_query($sql_p);
if(mysql_num_rows($query_p)!=0){
$row_p=mysql_fetch_array($query_p);
$_SESSION['cart'][$row_p['no']]=array("kuantiti" => 1);
}else{
$message="Product ID is invalid";
}
}
}
以下是将购物车中的商品插入数据库的代码:
if(isset($_SESSION['cart'])){
$sql = "SELECT * FROM peralatansukan WHERE no IN(";
foreach((array) $_SESSION['cart'] as $id => $value){
$sql .=$id. ",";
}
$sql=substr($sql,0,-1).") ORDER BY no ASC";
$query = mysql_query($sql);
if(!empty($query)){
while($row = mysql_fetch_array($query)){
$sql="INSERT INTO user_request(nama, noic, jawatan,
peringkat, email, no, peralatansukan, kuantiti) values
('$_SESSION[nama]', '$_SESSION[noic]',
'$_SESSION[jawatan]', '$_SESSION[peringkat]',
'$_SESSION[email]', '$row[no]', '$row[peralatansukan]',
'$row[kuantiti]')";
$res=mysql_query($sql);
unset($_SESSION['cart']);
echo '<script type="text/javascript">alert("Your request
is success.");window.location.href="viewalat.php";
</script>';
}}}
else{
echo '<script type="text/javascript">alert("Sorry.
Something went
wrong.");window.location.href="viewalat.php";
</script>';
}
我是PHP的新手。希望你能帮我摆脱这个问题。 Fyi,我还在学习使用mysqli。希望你能理解。谢谢。