kaosjon Posted August 30, 2011 Share Posted August 30, 2011 Hi, i am currently designing a new website and am testing a few different compnents of it. One part gathers two variables of the url using the GET method and selects the relevant data from the database. It then performs an UPDATE of a field called balance before INSERTING a new record. Everything works and gets inserted except for the UPDATE. Here is the code <?php include ("connect.php"); $id = ""; $offerid = ""; $id = $_GET['user_id']; $offerid = $_GET['offer_id']; $sql_user = mysql_query("SELECT * FROM user_info WHERE id = $id"); $sql_offer = mysql_query("SELECT * FROM offers WHERE offerid = $offerid"); $balance += $payout; mysql_query("UPDATE user_info SET balance = $balance WHERE id = $id"); mysql_query ("INSERT INTO offer_credited (id, offerid, date_credited, payout_to_user) VALUES ('$id', '$offerid', CURDATE(), '$payout')") or die(mysql_error()); echo "Your Offer has been recorded and Updated" ?> The balance field is a float that i want to increase by the amount of the payout variable which is from the offers table. However, the balance never gets updated and remains at 0. Any ideas what it could be, i am thinking it is something to do with, $balance += $payout; But i can't think of what else to do. Hopefully this isn't too confusing. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/246069-update-query-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted August 30, 2011 Share Posted August 30, 2011 Is $balance assigned a value at some point prior to $balance += $payout? $payout is likely undefined, making $balance empty, therefore the query fails. If $balance is supposed to come from one of the previous queries, note that all you have after mysql_query() is a result resource unless you do something with that resource using one of the mysql_fetch_[row/assoc/array] functions. $array = mysql_fetch_assoc($sql_offer); $balance = $array['payout']; Quote Link to comment https://forums.phpfreaks.com/topic/246069-update-query-not-working/#findComment-1263728 Share on other sites More sharing options...
kaosjon Posted August 31, 2011 Author Share Posted August 31, 2011 Is $balance assigned a value at some point prior to $balance += $payout? $payout is likely undefined, making $balance empty, therefore the query fails. If $balance is supposed to come from one of the previous queries, note that all you have after mysql_query() is a result resource unless you do something with that resource using one of the mysql_fetch_[row/assoc/array] functions. $array = mysql_fetch_assoc($sql_offer); $balance = $array['payout']; Hi, thanks for the reply, i done what you said and it seems to work slightly, it adds the payout to the balance and updates it, but when i tried it again, it should add the payout to the balance but it doesn't. In other words the first time i tried it the balance went from 0 to 1 because the payout is 1, however the second time i tried it the balance remained at 1 and did not change. It should have gone to 2. I am not sure what is not working in the code, any ideas? Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/246069-update-query-not-working/#findComment-1264036 Share on other sites More sharing options...
samshel Posted August 31, 2011 Share Posted August 31, 2011 mysql_query("UPDATE user_info SET balance = balance + $balance WHERE id = $id"); Quote Link to comment https://forums.phpfreaks.com/topic/246069-update-query-not-working/#findComment-1264049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.