-2

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;


           }
  • 2
    You should use prepared statements. This likely is open to SQL injections. – user3783243 Jun 19 '22 at 13:03
  • 1
    Just assigning "UPDATE" to a variable does not change anything to your database. Where is the `mysqli-execute` or the `PDO::exec` ? Please read [mre]. You should show your problem complete, not with only the parts you think are relevant. – Luuk Jun 19 '22 at 13:12
  • when i update it does update the value in my database correctly, just wont do it again – Jennifer Cornhill Jun 19 '22 at 13:17

2 Answers2

0

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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
-1
  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

Pelican
  • 14
  • 3