-1

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){

Shane
  • 244
  • 1
  • 4
  • 17

2 Answers2

1

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.

Alexander
  • 807
  • 5
  • 10
1

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)");

   }
}
Loko
  • 6,539
  • 14
  • 50
  • 78
  • @Shane Sorry instead of => use >= – Loko Nov 10 '13 at 16:08
  • I have updated this and now have a new error. Error: `Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/public_html/transfer.php on line 264` Line 264: `$select_result = $mysqli->query("SELECT * FROM member where personid=$accountfrom ");` – Shane Nov 10 '13 at 16:16
  • @Shane Damn sorry I am making such dumb mistakes. Put '' around $accountfrom – Loko Nov 10 '13 at 16:17
  • Mixing `mysqli_` with `mysql_` --- Not a good mix. – Funk Forty Niner Nov 10 '13 at 16:21
  • @Fred-ii- I know, he should use mysqli. Editted it. Didn't notice it before. – Loko Nov 10 '13 at 16:21
  • But you posted `$mysqli->query` and `$result = mysql_query` you made the initial mistake. @Loko – Funk Forty Niner Nov 10 '13 at 16:23
  • @Loko - Don't worry! you are been brilliant help! Please see updated question with my current code. I am now getting error 'Parse error: syntax error, unexpected T_VARIABLE in /home/public_html/transfer.php on line 264' – Shane Nov 10 '13 at 16:23
  • @Fred-ii- I know I just kind of copy pasted his query in it. I editted it now – Loko Nov 10 '13 at 16:24
  • @Shane edit your question with updated code. – Loko Nov 10 '13 at 16:25
  • @Shane dont use "" use ' ' at the $accountfrom. Use singles. – Loko Nov 10 '13 at 16:26
  • @loko Done that, Now got error `Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/public_html/transfer.php on line 267` – Shane Nov 10 '13 at 16:28
  • @Shane You also didn't change the => to >= I editted my code in my answer. Use it. – Loko Nov 10 '13 at 16:30
  • @loko Done. The page now shows no errors, however the amount is no longer transferring from account to account? – Shane Nov 10 '13 at 16:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40907/discussion-between-loko-and-shane) – Loko Nov 10 '13 at 16:31
  • @shane click on the link in my comment. – Loko Nov 10 '13 at 16:32