Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP MySQL Negative Value (Balance) Issue

    Greetings..
    I am developing a desktop software where it charge user per execution the main action. For example say it will charge user 0.1$ for per PDF print.

    and my software provide multithreading. .

    so, if it run single thread it works fine

    but the problem is if user run multiple thread at one (say 10/20 threads)

    it (php) also continues user to allow the server/execution even balance get below zero..

    though my php script check whether balance is positive ..

    but after user run multiple threads balance become like -5.95$ or -25.75$ etc

    and that is a big security/financial issue..

    here is the code I am using:
    Code:
    <?php
    	
    	$strSQL = "Select * from users where Email = '$strUser'";
    	$return = mysql_query($strSQL, $strDBConn);
    	$strDBData = mysql_fetch_array($return, MYSQL_ASSOC);
    	//checking balance
    	$strBalance = $strDBData['Balance'];
    	if($strBalance < 0)
    	{
    		// if balance 0 then exit so, my software/thread will not process further
    		mysql_close($strDBConn);
    		exit('Balance Exceed');	
    	}
    
    	//rest of the codes that realted to service executaion
    	
    	// code that substract the balnce
    		$dblCost = 0.25;
    		$strSQL = "Update users set Balance = Balance - '$dblCost' where Email = '$strUser'";
    		$return = mysql_query($strSQL, $strDBConn);
    
    	//rest finising codes 
    ?>
    any help/suggestion would be highly appreciated..

    thanks in advance.
    best regards

  • #2
    New Coder
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <?php
    	$dblCost = 0.25;
    
    $strSQL = mysql_query("SELECT *FROM user WHERE Email = '$strUser'";
      while($row = mysql_fetch_array($strSQL)) {
            echo "Your Balance: $" $row['Balance']  . "<br />";
            $newbalance=$row['Balance']-$dblCost; 
    
      If ($newbalance <= 0){
    	mysql_close();
    	exit('Balance Exceed');}
      else{
            mysql_query("UPDATE user SET Email='$newbalance', WHERE Email='$strUser'");
            echo "Your New Balance: $" $newbalance  . "<br />";}
    mysql_close();
    
    ?>
    
    Gojavo.com
    Tell me if this works. If it does perhaps you and I could work out some kind of a royalty deal off your software some where around 0.05% would be nice.

    Don't forget to give me a thanx

  • #3
    New Coder
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Be sure to put the } in at the end of the code to close the while loop forgot to put that in.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •