我正在製作購物車,當用戶向購物車中添加商品時,它將保存到數據庫中。我已將所有需要的信息放在標籤中。一些信息被重複爲純文本和隱藏輸入。問題是,當我添加一個項目到購物車時,它確實會說「添加項目」但實際上並沒有向數據庫添加任何信息。我已經測試過它是由圖像還是價格等引起的。它只在我離開order_quantity時才起作用。無法將信息從表單存儲到數據庫中
這裏是商店頁面:
<!DOCTYPE html>
<html>
<head>
<title>Store</title>
<link href="../styles/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<header>
<center><img src="../images/logo.png"></a></center>
<nav>
<ul>
<center><h3><li>Store</li>
<li><a href="cart.php">Cart</a></li>
<li><a href="userorders.php">My orders</a></li>
<li><a href="logout.php">Logout</a></li></h3></center>
</ul>
</nav>
</header>
<div class="left">
<ul>
<h4><li>Completes</li>
<li><a href="decksstore.php">Decks</li>
<li><a href="wheelsstore.php">Wheels</a></li>
<li><a href="bearingsstore.php">Bearings</a></li>
<li><a href="trucksstore.php">Trucks</a></li>
<li><a href="hardwarestore.php">Hardware</a></li></h4>
</ul>
</div>
<div class="main">
<br>
<?php
require_once('../../connect.php');
$select = "SELECT * FROM tblcompletes";
$response = mysqli_query($dbc, $select);
while ($row = mysqli_fetch_array($response))
{
echo '<div class="buyproducts">
<form method="post" action="addtocart.php">
<div style="border:1px solid white; background-color:white; border-radius:20px; padding:20px;" align="center">
<img src="data:image/jpeg;base64,'.base64_encode($row['product_image']).'" class="img-responsive">
<h4 class="text-info">'.$row['product_name'].'</h4>
<h4 class="text-danger">$'.$row['product_price'].'</h4>
<h4>'.$row['product_quantity'].' in stock</h4>
Quantity: <input type="text" name="order_quantity" class="form-control" value="1">
<input type="hidden" name="hidden_id" value="'.$row["product_ID"].'">
<input type="hidden" name="hidden_image" value="data:image/jpeg;base64,'.base64_encode($row['product_image']).'" class="img-responsive">
<input type="hidden" name="hidden_name" value="'.$row["product_name"].'">
<input type="hidden" name="hidden_price" value="'.$row["product_price"].'">
<input type="submit" style="margin-top:5px;" class="btn-success" value="Add to cart">
</div>
</form>
</div>';
}
?>
</div>
</body>
</html>
下面是操作文件:
<?php
require_once('../../connect.php');
$hidden_id = $_POST['product_ID'];
$hidden_image = $_POST['product_image'];
$hidden_name = $_POST['product_name'];
$hidden_price = $_POST['product_price'];
$order_quantity = $_POST['order_quantity'];
$user_email = $_COOKIE['user_email'];
if ($order_quantity == NULL)
{
echo '<script type="text/javascript"> alert("Please enter a valid value"); window.location="completesstore.php"; </script>';
}
else
{
$addtocart = "INSERT INTO tblorders (product_ID, product_iamge, product_name, product_price, order_quantity, user_email) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($dbc, $addtocart);
mysqli_stmt_bind_param($stmt, "issdis", $hidden_id, $hidden_image, $hidden_name, $hidden_price, $order_quantity, $user_email);
mysqli_stmt_execute($stmt);
echo '<script type="text/javascript"> alert("Item added"); window.location="completesstore.php"; </script>';
}
mysqli_st_close($stmt);
mysqli_close($dbc);
?>
在此先感謝。
你的錯誤是什麼? –
沒有錯誤;當表單提交時(使用添加到購物車按鈕),我無法將信息保存到數據庫。 – Dave
NSERT INTO商標(product_ID,product_iamge,product_name,product_price,order_quantity,user_email)VALUES(?,?,?,?,?,?) ----'''是給 ? – tousif