首页 > 解决方案 > 使用购物车页面中的 php 更新 sql 数量

问题描述

我有一个页面,用户可以在其中选择产品(产品来自 sql 数据库)并输入所需的数量。然后他们查看“购物车”页面并提交订单,该订单仅向我发送一封包含详细信息的电子邮件。我试图在他们提交订单时自动更新 sql 数据库中的数量。

我想我必须做类似以下的事情,但我是 php 和 sql 的新手。我必须从数据库中获取原始数量并减去用户输入并显示在购物车页面上的“订购数量”并设置该新值。谁能阐明我如何做到这一点?

我认为我必须做什么?:

$updquery = 'UPDATE "products" SET "Quantity"= "Quantity" - '. $product['quantity'] .' WHERE PID = '. $product['id'] .' ';

购物车.php

<?php
// Initialize the session
session_start();

// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
?>

<?php
@session_start();

if(isset($_POST['logout'])) {
unset($_SESSION['shopping_cart']);

}

if(isset($_POST['submit'])) {
$email = $_POST['email'];
// Create the email and send the message
$to = 'myemail@gmail.com'; // Add your email address inbetween the '' 
replacing yourname@yourdomain.com - This is where the form will send a 
message to.
$email_subject = "Products Order - ".$_POST['email']."";
// PREPARE THE BODY OF THE MESSAGE
        $email_body = '<html><body>';
        $email_body .= '<h1 style="text-align:center;">Products List</h1>';
        $email_body .= '<table rules="all" style="border-color: #666;margin: 
 auto;" border="1" cellpadding="10">';
        $email_body .= '<tr><th colspan="2"><h3>Requested Parts</h3></th> 
</tr>';
        $email_body .= '<tr><th width="100" align="left">Product ID</th><th 
width="100" align="right">Quantity</th></tr>';

         foreach($_SESSION['shopping_cart'] as $key => $product):

         $email_body .= '<tr><td>'. $product['id'] .'</td><td 
align="right">'. $product['quantity'] .'</td></tr>';

         endforeach; 
        $email_body .= "</table>";
        $email_body .= "</body></html>";
        $headers = "From: myemail@gmail.com\n"; // This is the email address 
the generated message will be from. We recommend using something like 
noreply@yourdomain.com.
        $headers .= "Reply-To: ".$_POST['email']."\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $headers .= "X-Mailer: PHP/".phpversion();   
 mail($to,$email_subject,$email_body,$headers);
unset($_SESSION['shopping_cart']);
?>
<script>
alert("Your order has been submitted")
 window.location.href = "index.php"
</script>
<?php
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<title>Inventory Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/icon.ico" type="image/x-icon">
<style>
*{
margin :auto;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}
tr:nth-child(even) {
background-color: #eee;
}  
tr:nth-child(odd) {
background-color: #fff;
}            
</style>

</head>
<body>

<h1 style="text-align:center;">Cart</h1>
<?php  
if(!empty($_SESSION['shopping_cart'])){  ?>
    <div class="table-responsive" style="background:white;">  
    <table class="table table-responsive" id="mytable" border="1" 
align="center">  
        <tr><th colspan="2"><h3>Requested Parts</h3></th></tr>  
    <tr>  
         <th width="100" align="left">Product ID</th>  
         <th width="100" align="right">Quantity</th>  
    </tr>  
 <?php


         foreach($_SESSION['shopping_cart'] as $key => $product):
    ?>  
    <tr>  
       <td><?php echo $product['id']; ?></td>  
       <td align="right"><?php echo $product['quantity']; ?></td>  
    </tr>  
    <?php  
         endforeach;  
    ?>  


    </table>
    <div style="text-align:center;">

    <form action="" method="post">
      <input type="email" size="31" name="email" placeholder="Please enter 
your email address" required /><br>
      <input type="submit" name="submit" value="Submit" id="btnSubmit"/>
    </form>
    <form action="" method="post">
      <input type="submit" name="logout" value="Clear" id="btnClear" />

    </form>

    </div>
     </div>
    <?php  
 }
    else{
        ?>
        <script>
            alert("Cart is empty")
            window.location.href = "index.php"
        </script>
        <?php
    }
    ?>
    <br><br>
 </body>
</html>

标签: phpsqlcartupdating

解决方案


您提供了自己的答案(查询):

1)在脚本开头设置mysql连接:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

2) 在您的foreach中,添加查询以在您将产品添加到电子邮件时更新每个操作系统的产品数量。变成:

foreach($_SESSION['shopping_cart'] as $key => $product):
   $email_body .= '<tr><td>'. $product['id'] .'</td><td align="right">'. $product['quantity'] .'</td></tr>';
   mysqli_query($con,'UPDATE products SET Quantity = Quantity - '. $product['quantity'] .' WHERE PID = '. $product['id'] .' ');
endforeach; 
mysqli_close($con);

推荐阅读