更新数据库,然后一旦PHP

发布于 2025-02-08 15:45:13 字数 438 浏览 1 评论 0原文

我总金额。当我出售AM商品时,我想将该金额添加到我的总数中。例如,我有100.00,我以100.00的价格出售蛇,因此我的总数应该为200.00。我的工作正常,所以它增加了100.00,但是如果我想出售两条蛇,它将保持在200.00,它不会增加第二条蛇的任何数量。使用它来更新我的数据库。

if(isset($_POST['go'])
              { 
 $query = "UPDATE  users  SET amount=$current WHERE email = '".$_SESSION['email']."'";

}

           if(isset($_POST['go'])      //edit 17.06.2022
           {  
                    $current = $amount+100;


           }

I have a total amount. when i sell am item i want to add that amount to my total. e.g i have 100.00, i sell a snake for 100.00 so my total should be 200.00. I have it working so it adds the 100.00 but if i want to sell two snakes it will stay at 200.00 it wont add any amount from the second snake. using this to update my database.

if(isset($_POST['go'])
              { 
 $query = "UPDATE  users  SET amount=$current WHERE email = '".$_SESSION['email']."'";

}

           if(isset($_POST['go'])      //edit 17.06.2022
           {  
                    $current = $amount+100;


           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2025-02-15 15:45:13

您的代码运行在顺序中,因此对$ current进行的更改将无法保存在任何地方当前请求。

如果要更新数据库中的值,则需要在SQL查询之前将计算移至

Your code runs in order, so changes made to $current after you've run the SQL won't be saved anywhere, they'll just be thrown away at the end of the current request.

If you want to update the value in the database, you need to move the calculation to before the SQL query.

始终不够 2025-02-15 15:45:13
  1. 您需要在之前进行计算您使用计算的金额,即
  2. 要执行SQL语句
if(isset($_POST['go'])
{
 $current = $amount+100;
 $query = "UPDATE  users  SET amount=$current WHERE email ='".$_SESSION['email']."'";
$result=$db->exec($query);
}  
      

$ db的$当前数量是从下面的连接文件中

<?php

  $db=new PDO('mysql:host=localhost;dbname=testdb;charset=utf8','root','');
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
?>

添加以下行,然后与DB进行交互。

require('connection.php');

当您让它运行时,建议您查看准备的陈述以防止应用程序上的SQL注入漏洞

  1. You need to do the calculation before you use the calculated amount, which is $current
  2. You need to execute the sql statement
if(isset($_POST['go'])
{
 $current = $amount+100;
 $query = "UPDATE  users  SET amount=$current WHERE email ='".$_SESSION['email']."'";
$result=$db->exec($query);
}  
      

$db is from the connection file below

<?php

  $db=new PDO('mysql:host=localhost;dbname=testdb;charset=utf8','root','');
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
?>

Add the below line before interacting with the DB.

require('connection.php');

When you get it to run, I suggest looking at prepared statements to prevent SQL injection vulnerabilities on your app

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文