PHP Script error :Subtracting two variable failure

Hello everyone! I have this issue since yesterday and I can’t still figure it out. I subtract the current load balance and ticket price but I always got a wrong result. Like for instance, the current load balance is 1000 and the ticket price is 54, the difference must be 946 but I always got 622 which is 7x from the ticket price. Can you please check my algorithm and tell me what’s wrong. :sweat_smile:

This is my source code:

 $conn = mysqli_connect($host,$username,$password,$dbname);
    $sel = mysqli_query($conn,"SELECT * FROM ticket WHERE Cus_IDNum='$IDNum' AND Bus_Plate_Number='$PlateNum' AND Tk_Sch_Date = CURRENT_DATE()");
    while ($rows=mysqli_fetch_array($sel))
    {
        $Tk_Stat = $rows['Tk_Stat'];
        $Tk_Price = $rows['Tk_Price'];
        
        if($Tk_Stat = "UNPAID")
        {
           $sel2 = mysqli_query($conn,"SELECT * FROM customers WHERE Cus_IDNum='$IDNum'");
           while ($rows=mysqli_fetch_array($sel2))
           {
               $currbal = $rows['Cus_Loadbal'];
               if($currbal > $Tk_Price)
               {
                   $totalbal = $currbal - $Tk_Price;
                   
                  $upd = mysqli_query($conn,"UPDATE ticket SET Tk_Stat = 'PAID' WHERE Cus_IDNum ='$IDNum'");
        	      if($upd)
        	      {
                      $upd2 = mysqli_query($conn,"UPDATE customers SET Cus_Loadbal = '$totalbal' WHERE Cus_IDNum ='$IDNum'");
            	      if($upd2)
            		  {
            		    $upd3 = mysqli_query($conn,"UPDATE qrcode SET Cus_Loadbal = '$totalbal' WHERE Cus_IDNum ='$IDNum'");
                		if($upd3)
                		{
                		  print "1";
                		}
            	      }
        	      }
               }
               else
               {
                   print "0";
               }
            }
        }
        else if($Tk_Stat = "PAID")
        {
           print "1";
        }
    }
            

If you found something let me know. Thanks in advance :sweat_smile:

1 Like

can you print the both variable before subtracting and post the result here. just echo $currbal and echo $Tk_Price or just print_r($rows);

Still the same
I already put it in this way:

echo $currbal;
echo $Tk_Price;
$totalbal = $currbal - $Tk_Price;

the value of currbal is 946 and the tk price is 54, I got 784 instead of 892.

This is happening because you are subtracting it inside the loop.
Subtract them outside the loop

1 Like

I tried right now but this time I got a higher value.
I remove the while loop then I run it again, the balance is 1000 and the price is 54, I got 989 instead of 946
this is the code:

$conn = mysqli_connect($host,$username,$password,$dbname);
    $sel = mysqli_query($conn,"SELECT * FROM ticket WHERE Cus_IDNum='$IDNum' AND Bus_Plate_Number='$PlateNum' AND Tk_Sch_Date = CURRENT_DATE()");
    
        $rows=mysqli_fetch_array($sel);
        $Tk_Stat = $rows['Tk_Stat'];
        $Tk_Price = $rows['Tk_Price'];
        
        if($Tk_Stat = "UNPAID")
        {
           $sel2 = mysqli_query($conn,"SELECT * FROM customers WHERE Cus_IDNum='$IDNum'");
           if($sel2)
           {
               $rows=mysqli_fetch_array($sel2);
               $currbal = $rows['Cus_Loadbal'];
               if($currbal > $Tk_Price)
               {
                   echo $currbal;
                   echo $Tk_Price;
                   $totalbal = $currbal - $Tk_Price;
2 Likes

@Gelo_Reuben Check PM

1 Like

Here is Kodular Community.

Your questions are about:

1-basic concepts of programming logic

2-PHP

One more time , I will suggest to you :

https://adriann.github.io/programming_problems.html

https://www.phpexercises.com/

2 Likes