0

I need to get a balance variable out from a table and insert it back to the same table to different value, i am not sure am making it right as SOL is asking me to check the right syntax to use.

This is my full php script

   <?php

// Connects to the database 

$db = mysql_connect("$Sname","$Uname","$Pname") or die("Could not connect to the Database."); 
$select = mysql_select_db("$Dname") or die("Could not select the Database."); 


//Converts form values to simple variables 
if ($_POST['submitButton']) {
$amount = $_POST['amount'];

$today = date("d/m/Y");
$time = date("h:i A");

$sql = "SELECT * FROM $UUname WHERE full='".$amount."'"; 
$result = mysql_query($sql) or die(mysql_error()); 
$num_return = mysql_num_rows($result); 
if ($num_return == 1){
// inserts the values into the DB 

mysql_query("UPDATE $TATname SET date='".$today."', time='".$time."', amount='".$amount."' WHERE user='{$_SESSION['user']}'");


$user = "{$_SESSION['user']}"; 

// get credits

$query = mysql_query("SELECT SUM(credit) FROM $UUname WHERE user='".$user."'");
$credit = mysql_result($query, 0);


// get debits

$query = mysql_query("SELECT SUM(debit) FROM $UUname WHERE user='".$user."'");
$debit = mysql_result($query, 0);


$bal = $credit - $debit;

$query = "UPDATE $UUname SET full='".$bal."'"; 
$update = mysql_query($query) or die(mysql_error()); 

    header("Location: successful.php");
    exit;
}
    else
{
    echo "Insufficient Funds";
}
}
?>

Please what is the right syntax to use on that last line, or what is the right way for me to do this, because i want to tell users that they have insufficient balance when they try to make a transfer.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • You want to `UPDATE` a field, not `INSERT` a new one, right ? – Clément Malet Aug 08 '14 at 10:01
  • ya and also what $amount containing because you are getting your balance amount in $bal then what $amount belongs to. – Ricky Aug 08 '14 at 10:03
  • yeah i need to get the balance and insert it in an empty value which is full then use it to process another transfer to validate how much the user want to transfer and how much will be there before saying insufficient balance, so what i did was the line $bal = $credit - $debit, then get the balance which is $bal and insert it into full value, i don't know if am doing it right, but if you have a good way to tell users that they have insufficient balance please let me know – user3919514 Aug 08 '14 at 10:05
  • @Ricky, is an empty value i needed to use it to process next transaction to tell the user that they have insufficient balance, which is when next time they try to make transfer it will check how much they want to transfer and how much is remaining, i don't think this is the right way to do this if you any other way please let me know – user3919514 Aug 08 '14 at 10:08
  • if you write insert query then it will add new row of record i think you must want to update the field for the bal amount for column name full – Ricky Aug 08 '14 at 10:08
  • ok so please post your table example so that we can clearly understand it. – Ricky Aug 08 '14 at 10:10
  • maybe i am making it wrong because i checked my table value and what it inserted is the amount that is already there which is the same amount that i entered to be transferred, – user3919514 Aug 08 '14 at 10:21

2 Answers2

0

you must write query like this

$query = "UPDATE $UUname SET full='".$bal."'"; 
$update = mysql_query($query) or die(mysql_error());
Ricky
  • 284
  • 1
  • 3
  • 19
0

It's not clear from your post, but I assume you have one table $UUname which contains columns named credit, debit and full (the latter containing the balance)?

If that is the case, the correct way to update the balance column would be:

$query = "UPDATE $UUname SET full = $bal";

If that is not the case, please provide your table layout to be more specific.

UPDATE

Not sure if I fully understood your problem, but from what I got a solution could look like this:

Create a user table:

user_id | username | balance

user_id is primary key and there is only one row for each user. You can add further user related attributes as you like.

Create a transaction table:

transaction_id | user_id | pdate | bdate | amount | text

When a user adds credit, you add a row with a positive amount, when he is "purchasing" something, you add a negative amount. "text" is simply a description of the transaction, like so:

User receives credit of 10000:

INSERT INTO transaction_table (user_id, pdate, bdate, amount, text) VALUES
('12345', '2014-08-08', '2014-08-08', 10000, "Credit charge");

User spends 5000:

INSERT INTO transaction_table (user_id, pdate, bdate, amount, text) VALUES
('12345', '2014-08-08', '2014-08-08', -5000, "bought some cool item");

Now when you add up all amounts for a user, you receive the user's balance:

SELECT user_id, SUM(amount) FROM transaction_table GROUP BY user_id;

or, for a single user with user_id 12345:

SELECT SUM(amount) FROM transaction_table WHERE user_id='12345';

You can update the balance in the user table like that:

UPDATE user_table
   SET balance=$balance
WHERE user_id=12345;

Now, when you perform a transaction, you simply check if the balance is sufficient for the amount due. If it is, you proceed; if not, you stop.

That should provide a good compromise between simplicity and integrity for such a use case. You could also do a full update for all user balances using one sql statement (e.g. analog to https://stackoverflow.com/a/10095710/1140979). Hope this helps.

Community
  • 1
  • 1
JoeGo
  • 293
  • 2
  • 12
  • but i want to also want you to know that i have store a remaining balance after user make a transfer to the full field in transaction table and when a user make a transfer the remaining balance store there which works fine, but what i need now is how to use it to process a transfer to check that full field how much is there and how much the user want to transfer, because the way i did it is only check if that field is empty or it matched the inputted amount if it matched the input amount proceed but if it does not matched. – user3919514 Aug 08 '14 at 12:50
  • the inputted amount echo insufficient fund, which will also echo insufficient if the full field is empty – user3919514 Aug 08 '14 at 12:51
  • wait, are users transferring credits/debits between each other? – JoeGo Aug 08 '14 at 12:52
  • i just need a simple code like when a user have 10,000 and decide to make a transfer of 5000 let it proceed. And don't proceed only when the user balance is insufficient or trying to make a higher amount that is in his/her balance, something like when a user have 10,000 and try's to make a transfer of 12,000 don't proceed unless the user has that kind of amount in their balance, if they try to make a transfer of 12,000 echo insufficient fund to them, only between from 10,000 to down and 0 is insufficient. – user3919514 Aug 08 '14 at 13:06
  • because in my code the user will only make a transfer of the total balance like when a user have 10,000 and decide to make transfer of 5,000 in my code it will say you have insufficient fund, unless the user try to make the whole amount which is the 10,000, that is how i validate it, just like a username and password but that's not the way i need it, just a simple code that will allow the user to make a transfer below his/her amount and refuse when they try to make a higher amount that is not of their amount, and also refuse when they try to make a 0 amount – user3919514 Aug 08 '14 at 13:09
  • It's hard for me to understand your problem. When you always keep the balance in the user table up to date, you just have to check if this value is >= the amount the user wishes to transfer. If it is, proceed, if not, block the transaction. Straightforward. – JoeGo Aug 08 '14 at 13:21
  • $sql = "SELECT * FROM $TTname WHERE credit='".$amount."'"; $result = mysql_query($sql) or die(mysql_error()); $num_return = mysql_num_rows($result); if ($num_return == 1){ proceed } else { echo "Insufficient Fund"; } is this correct this is how i am validating the entered amount with the stored amount – user3919514 Aug 08 '14 at 13:30
  • Geez, you are allowed to think a little bit yourself, you know that? :-) Well, I suggested to change your table schema and if you change it, you of course you have to adjust your logic accordingly. What you would do is e.g.: `SELECT balance FROM user_table where user_id=12345` and use the returned value to check your condition. Provided you always update the balance in the user table when the transaction table changes. Btw: if this answer helped you along, please don't forget to accept it. – JoeGo Aug 08 '14 at 13:58
  • yes of course the balance updates anything the user make a transfer, but what i want to know will the balance be empty or the store amount has to be there because if it is empty it won't proceed, and if the amount is there is 10,000 and the user try to transfer 5,000 i don't think it will proceed, just saying but let me try this post – user3919514 Aug 08 '14 at 14:08
  • well i tried it now i made a transfer of 5000 from 10000 and the balance is 5000, but it didn't validate when i try to make a transfer of 10000 from it again, i know is not what the balance is but it should say sorry you have insufficient fund, until i can make a transfer from below 5000 as what is inside the balance. – user3919514 Aug 08 '14 at 14:18
  • i know you are not understanding me, but this is too much all i just hope i can get a validation php code, that will check how much they user try to transfer and how much is the user balance, if the amount the user try's to transfer is equivalence to how much the user have proceed. But if it is 0 balance or higher than the amount the user try to transfer tell the user that he/she have an insufficient amount or balance compare to the amount he/she want to transfer – user3919514 Aug 08 '14 at 14:22
  • I think you need to redesign your solution to arrive at a consistent modelling of your business logic. After that, checking the balance will be simple and reliable. I'm sorry, but I don't understand (literally) what the problem is with checking the balance. – JoeGo Aug 08 '14 at 14:31