Search code examples
phpmysqlperformancesumconnection

how to sum up a specific cell and replace it with the new one php


I got a PHP code like this

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$ID=$_POST["ID"];

$Deposit=$_POST["Deposit"];

$sum=0;

$sql="SELECT Current_Balance FROM users WHERE id='".$ID."'";
$result = mysqli_query($conn, $sql);


$y=$Deposit;

 $sum +=  (int)$sql + (int)$y;

echo "New Balance is: ",$sum;

I want to take a specific number from my cell and sum it up with a user's input number and then replace the cell's specific number with the new one. Any idea why this is not working?


Solution

  • Your trying to convert your text query into an int.

    $sum +=  (int)$sql + (int)$y;
    

    Your

    $result = mysqli_query($conn, $sql);
    

    Should be

    $query = mysqli_query($conn, $sql); //asks the question
    $result= mysqli_fetch_assoc($conn,$query);//gets the answer 
    $sum +=  $result['Current_Balance'] + (int)$y;
    echo "New Balance is: ".$sum;