我正在尝试创建一个购物车。我试图得到的总数 输出,但返回为0输出应该是价格*数量,但结果是0,这些是我已经尝试过的东西。我正在尝试创建一个购物车。我试图得到的总数 输出,但返回为0,输出应该是价格*数量,但结果是0,这些是我已经尝试过的东西。
<?php
include 'config.php';
{
if (isset($_POST["cart"])){
$field0 = $_POST["field0"];
$field1 = $_POST["field1"];
//$field2 = $_POST["field2"];
$field3 = $_POST["field3"];
$field4 = $_POST["field4"];
$field5 = $_POST["field5"];
$or_qty = $_POST["or_qty"];
$price = $_POST["price"];
if (empty($field0)||empty($field1)||empty($field3)||empty($field4)||empty($field5)){
//echo '<script>alert("Please Complete all the data")</script>';
}else{
$insert = mysqli_query($conn, "INSERT INTO `template` (`field0`,`field1`,`field3`,`field4`,`field5`,`or_qty`) VALUES ('$field0','$field1','$field3','$field4','$field5','$or_qty')");
//echo '<script>alert("Order is added to cart")</script>';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cart</title>
</head>
<body>
<?php
$total = 0;
if ($field0 == 1){
$total = $total + ($or_qty);
}
elseif($field0 == 2){
$total = $total + ($or_qty*$price);
}
elseif($field0 == 3){
$total = $total + ($or_qty*$price);
}
elseif($field0 == 4){
$total = $total + ($or_qty*$price);
}
echo number_format($total);
$insert = mysqli_query($conn, "INSERT INTO `product_order` (`total`,`or_Qty`,`price`) VALUES ('$total','$or_qty','$price')");
?>
<!--$total = $total + ($values["item_quantity"] * $values["item_price"]);-->
<?php } ?>
</body>
</html>
答案 0 :(得分:0)
通过给$ _POST [“ cart”]一个值并手动设置$ field0,$ or_qty和$ price,我能够使$ total等于0以外的值。 如果我删除$ _POST [“ cart”] = 3 ;,则总数始终为0。
我还添加了一些逻辑以确保只有在设置了$ _POST [“ cart”]并稍微清理代码的情况下才显示总数。
我认为您的问题是未设置$ _POST [“ cart”],因此也从未设置其余变量。
通常$ _POST值是通过表单或其他网页设置的。
Finally, you should consider using prepared statements to prevent mysql injections.
<?php
include 'config.php';
$_POST["cart"]=3;
if (isset($_POST["cart"])){
$field0 = 3;
$field1 = $_POST["field1"];
//$field2 = $_POST["field2"];
$field3 = $_POST["field3"];
$field4 = $_POST["field4"];
$field5 = $_POST["field5"];
$or_qty = 4;
$price = 0.50;
if (empty($field0)||empty($field1)||empty($field3)||empty($field4)||empty($field5)){
//echo '<script>alert("Please Complete all the data")</script>';
}else{
$insert = mysqli_query($conn, "INSERT INTO `template` (`field0`,`field1`,`field3`,`field4`,`field5`,`or_qty`) VALUES ('$field0','$field1','$field3','$field4','$field5','$or_qty')");
//echo '<script>alert("Order is added to cart")</script>';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cart</title>
</head>
<body>
<?php
if (isset($_POST["cart"])){
$total = 0;
if ($field0 == 1){
$total = $total + ($or_qty);
}
elseif($field0 == 2){
$total = $total + ($or_qty*$price);
}
elseif($field0 == 3){
$total = $total + ($or_qty*$price);
}
elseif($field0 == 4){
$total = $total + ($or_qty*$price);
}
echo number_format($total);
$insert = mysqli_query($conn, "INSERT INTO `product_order` (`total`,`or_Qty`,`price`) VALUES ('$total','$or_qty','$price')");
}
?>
<!--$total = $total + ($values["item_quantity"] * $values["item_price"]);-->
</body>
</html>