Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,411 members, 7,954,634 topics. Date: Saturday, 21 September 2024 at 03:33 AM

Figure Out The Error Please (PHP And Mysql)!!! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Figure Out The Error Please (PHP And Mysql)!!! (1595 Views)

Please Php Gurus, Help Me / Android Quiz Application With Json Parser, Php And Mysql Database / Why Do Seun Dont Like Php And Mysql Programmers (2) (3) (4)

(1) (Reply) (Go Down)

Figure Out The Error Please (PHP And Mysql)!!! by Dsense(m): 2:29pm On Aug 24, 2012
This is a web application that is meant to conduct transaction after being connected to Database. But, it seems i still haven't positioned the BEGIN and COMMIT perfectly. Please drop your input if any!


if(isset($_POST['amount']))
{
if(isset($_POST['ADD']) && $number !=""wink
{
mysql_query("BEGIN"wink;
$query="UPDATE accounts SET balance='balance+amount' WHERE number='12345'";
mysql_query($query);
}
else
{
$query="UPDATE accounts SET balance='balance-amount' WHERE number='12345'";
mysql_query($query);
}

mysql_query("COMMIT"wink;
Re: Figure Out The Error Please (PHP And Mysql)!!! by Nobody: 2:43pm On Aug 24, 2012
what's the error you get obviously we don't have the same table you have. why not throw an exception there and see what you get.
Re: Figure Out The Error Please (PHP And Mysql)!!! by Dsense(m): 3:18pm On Aug 24, 2012
pc guru: what's the error you get obviously we don't have the same table you have. why not throw an exception there and see what you get.

Thanks for the concern.
Acutally, no error is being displayed, but i expect the the balance to have increased or decreased in the database, and which is not happening. I guess the issue is more like a semantic error not syntactic.
Re: Figure Out The Error Please (PHP And Mysql)!!! by Nobody: 5:53pm On Aug 24, 2012
mysql_query() commits queries automatically so no need to commit changes, you also want to remove the mysql_query("BEGIN" );

this line

$query="UPDATE accounts SET balance='balance+amount' WHERE number='12345'"; 

does not make sense, even if balance and amount were constants, it still wont work that way
i believe you wanted to do something like this

$query="UPDATE accounts SET balance='$balance+$amount' WHERE number='12345'"; 

which will still not give you the desired result
why not add things up before append the query like this


$finalBalance=$balance+$amount;
$query="UPDATE accounts SET balance='$finalBalance' WHERE number='12345'";

remember, if the balance column in your database is of type INT, then you want remove the quote, same with the quote around 12345


I also want to believe you have selected your database before trying that query.
Re: Figure Out The Error Please (PHP And Mysql)!!! by Nobody: 6:32pm On Aug 24, 2012
to be on the safe side always prefer to evaluate the expression and save the result to the db its cleaner that way
Re: Figure Out The Error Please (PHP And Mysql)!!! by DualCore1: 7:55pm On Aug 26, 2012
D-sense:
This is a web application that is meant to conduct transaction after being connected to Database. But, it seems i still haven't positioned the BEGIN and COMMIT perfectly. Please drop your input if any!


if(isset($_POST['amount']))
{
if(isset($_POST['ADD']) && $number !=""wink
{
mysql_query("BEGIN"wink;
$query="UPDATE accounts SET balance='balance+amount' WHERE number='12345'";
mysql_query($query);
}
else
{
$query="UPDATE accounts SET balance='balance-amount' WHERE number='12345'";
mysql_query($query);
}

mysql_query("COMMIT"wink;

Look at what I have put in bold. That's not correct variable declaration.
What you should have there is

$query="UPDATE accounts SET balance = balance + '$amount' WHERE number='12345'";

AND

$query="UPDATE accounts SET balance = balance - '$amount' WHERE number='12345'";

Assuming the variable holding the amount is $amount.
i.e $amount = $_POST['amount'];



Then again, although the whole MySQL BEGIN thing is not necessary and should be removed its still good to point to you that you have placed it in a wrong place. If it was necessary to use the BEGIN command, it should have been placed outside the IF block checking if the operation is an addition. You have placed yours inside the IF block that will only run if the operation is an addition. So if the operation is not addition, the BEGIN query wont run. Discard the BEGIN and keep the principle in mind for future tasks.
Re: Figure Out The Error Please (PHP And Mysql)!!! by silverluci(f): 2:56pm On Sep 04, 2012
webdezzi: mysql_query() commits queries automatically so no need to commit changes, you also want to remove the mysql_query("BEGIN" );

this line



does not make sense, even if balance and amount were constants, it still wont work that way
i believe you wanted to do something like this



which will still not give you the desired result
why not add things up before append the query like this





remember, if the balance column in your database is of type INT, then you want remove the quote, same with the quote around 12345


I also want to believe you have selected your database before trying that query.


your head is there jare!

(1) (Reply)

10 Programming Languages You Should Learn In 2014 / BREAKING: Mark Zuckerberg Is Back In Nigeria To Attend Aso Villa Demo Day / .

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.