doulaopu2343 2013-11-10 12:59 采纳率: 100%
浏览 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)");
    
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!