Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从购物车页面更新sql数量_Php_Sql_Cart_Updating - Fatal编程技术网

使用php从购物车页面更新sql数量

使用php从购物车页面更新sql数量,php,sql,cart,updating,Php,Sql,Cart,Updating,我有一个页面,用户从sql数据库中选择产品,并输入所需数量。然后他们查看购物车页面并提交订单,该订单只会向我发送一封包含详细信息的电子邮件。我试图在他们提交订单时自动更新sql数据库中的数量 我想我必须做如下的事情,但我对php和sql是新手。我必须从数据库中获取原始数量,减去用户输入并显示在购物车页面上的订购数量,然后设置新值。有人能告诉我如何做到这一点吗 我想我该怎么办 $updquery = 'UPDATE "products" SET "Quantity"= "Quantity" - '

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

我想我必须做如下的事情,但我对php和sql是新手。我必须从数据库中获取原始数量,减去用户输入并显示在购物车页面上的订购数量,然后设置新值。有人能告诉我如何做到这一点吗

我想我该怎么办

$updquery = 'UPDATE "products" SET "Quantity"= "Quantity" - '. $product['quantity'] .' WHERE PID = '. $product['id'] .' ';
cart.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>

您提供了自己的问题答案:

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);

这就是我最初的想法和尝试。我没有收到任何错误,但数据库根本没有更新。有什么想法吗?请重试查询,我删除了Quantity和table name products中的双引号。仍然没有运气:。当我使用解析值在phpmyadmin中单独运行查询时,查询就会工作。从电子邮件中我知道这些值正在解析。这快把我逼疯了!将查询更改为select并转储它,如下所示:$test=mysqli\u query$con,select*from products;var_dump$测试;以确保100%连接成功。objectmysqli_结果[2]公共“当前_字段”=>int 0公共“字段”=>int 5公共“长度”=>null公共“num_行”=>int 152公共“类型”=>int 0