我正在開發一個桌面軟件,向用戶收取每次執行的主要操作。例如,說每個PDF打印將向用戶收取0.1美元。PHP MySQL負值(餘額)問題
和我的軟件提供多線程。 。
因此,如果運行單線程它工作得很好:)
但問題是,如果用戶同時運行多個線程在一個(比如10/20線程)
它(PHP)也繼續向用戶允許服務器/執行甚至得到平衡零下..
雖然我的PHP腳本查詢餘額是正..
但畢竟用戶同時運行多個線程的平衡變得像-5.95 $或-25.75 $等
,這是一個很大的安全/財務問題..
這裏是我使用的代碼:
<?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
>
任何幫助/建議將高度讚賞..
在此先感謝。 問候
開始使用SQL事務。在這裏閱讀更多http://dev.mysql.com/doc/refman/5.0/en/commit.html – bodi0
也...注意[sql注入](http://stackoverflow.com/questions/60174/how如果您擔心安全性,請使用 –
不要再使用PHP的'mysql'擴展名,因爲它已被棄用,並且會被下一個刪除PHP版本。改用「PDO」或「mysqli」。 – TiMESPLiNTER