doulaopu2343
2013-11-10 12:59
浏览 7
已采纳

转账余额[重复]

This question is an exact duplicate of:

I am building a site where users can transfer funds to one another.

I have my code working so each user can send funds, however i'm not sure how to only let them transfer what they have.. (if they have 10 cant send 11)

See code below

$result = mysql_query("UPDATE member
SET balance = IF(personID = $accountfrom, balance-$amount, balance+$amount)
WHERE personID IN ($accountfrom, $accountto)")
 or die(mysql_error());

Can anyone help with this??

UPDATED

$select_result = $mysqli->query("SELECT * FROM member where personID="$accountfrom" ");
while ($select = mysqli_fetch_assoc($select_result)) {
   $balance=$select['amount'];
   if($balance=>$balance){
       $result = mysql_query("UPDATE member
       SET balance = IF(personID = $accountfrom, balance-$amount, balance+$amount)
       WHERE personID IN ($accountfrom, $accountto)")
       or die(mysql_error());
   }
}

Shows error:

Parse error: syntax error, unexpected T_VARIABLE in >/home/u704855438/public_html/transfer.php on line 264

Line 266 is if($balance=>$amount){

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjuete9206 2013-11-10 14:10
    已采纳

    Just check in your database wether the amount he wants to transfer is lower or the same as the amount he has. If it doesn't dont transfer it (so dont do anything and show an error message) Just put an

    if($amount_in_database>=$amount_he_wants_to_transfer){
    
    }
    

    around your update query.

    To get the amount in the database do this:

    $select_result = $mysqli->query("SELECT * FROM member where personid='$accountfrom' ");
    

    Afterwards use this where ['amount'] should be your column with the funds:

    while ($select = mysqli_fetch_assoc($select_result)) {
    $amount_in_database=$select['amount'];
    }
    

    So this would be your code:

    $select_result = $mysqli->query("SELECT * FROM member where personid=$accountfrom ");
    while ($select = mysqli_fetch_assoc($select_result)) {
       $amount_in_database=$select['amount'];
       if($amount_in_database>=$amount_he_wants_to_transfer){
           $result = $mysqli->query("UPDATE member
           SET balance = IF(personID = $accountfrom, balance-$amount, balance+$amount)
           WHERE personID IN ($accountfrom, $accountto)");
    
       }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanjian4150 2013-11-10 13:54

    You need to check before transfer an available funds, what "user from" can do this and his balance greater than transfer amount, or equals.

    MySQL tables which is using engine InnoDB are transactions safe. After updating you need to execute "commit" command in case if all processed correct, otherwise should be "rollback". In this case you will have integrity of database.

    Additionally to this make sense to have a transaction log table, where will be logged all transactions. For example you can have following columns personFromId, personToId, amount, transactionDate. And for one transfer between users will created 2 rows with +amount and -amount.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题