我正在制作购物车,当用户将商品添加到购物车时,它会保存到数据库中。我已在标签中放置了所有必需的信息。一些信息以纯文本和隐藏输入重复。问题是当我向购物车添加商品时,它确实说“已添加商品”,但实际上并未向数据库添加任何信息。我已经测试过它是由图像还是价格等造成的。只有当我只留下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);
?>
提前致谢。
答案 0 :(得分:0)
更改
$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'];
要
$hidden_id = $_POST['hidden_id'];
$hidden_image = $_POST['hidden_image'];
$hidden_name = $_POST['hidden_name'];
$hidden_price = $_POST['hidden_price'];
$order_quantity = $_POST['order_quantity'];
$user_email = $_COOKIE['user_email'];
name
中使用的 form
属性在 addtocart.php 文件中有所不同。
答案 1 :(得分:0)
使用以下代码更新您的操作文件:
$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'];
将上述代码替换为:
$hidden_id = $_POST['hidden_id'];
$hidden_image = $_POST['hidden_image'];
$hidden_name = $_POST['hidden_name'];
$hidden_price = $_POST['hidden_price'];
$order_quantity = $_POST['order_quantity'];
$user_email = $_COOKIE['user_email'];
答案 2 :(得分:0)
在按钮示例中添加一些名称:
<input type="submit" style="margin-top:5px;" class="btn-success" value="Add to cart" name="Submit">
然后到你的addtocart.php添加:
if(isset($_Submit['Submit'])){
$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'];}
并确保您已连接到数据库。