jmrothermel Posted June 12, 2008 Share Posted June 12, 2008 Ok I have a few commands going on at once. All seem to be executing except the first UPDATE. $update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'"); Could anyone tell me why that might be? Here is the code I am using. if($a == 'feed') { $user = $_GET['$username']; $date = date("y-m-d"); $check= mysql_query("SELECT * FROM pets WHERE `user`='$username'"); $check_num= mysql_num_rows($check); if ($check_num == "0"){ print "Record could not be found"; }else{ $sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'"); $new = $sql - 5000; $update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'"); $new2 = $MONEY + 5000; $insert=mysql_query("INSERT INTO `petsfed` SET `user`='$u', `fed` = '$username', `date` = '$date'"); $update2=mysql_query("UPDATE `members` SET `money`='$new2' WHERE `username`='$u'"); $insert2=mysql_query("UPDATE `pets` SET `lastfed`='$date' WHERE `user`='$username'") or die (mysql_error()); if($insert && $insert2 && $update && $update2) { printf ("Pets Fed: %d\n", mysql_affected_rows()); Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/ Share on other sites More sharing options...
rhodesa Posted June 12, 2008 Share Posted June 12, 2008 change it to this and see if it displays any errors for you: $update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'") or die("Update error: ".mysql_error()); EDIT: Also, use use $user on the second line, and $username in the rest of the code. Should line 2 be $username? Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/#findComment-563961 Share on other sites More sharing options...
jmrothermel Posted June 17, 2008 Author Share Posted June 17, 2008 Hmmmmm I dont get any errors no, but it still doesnt update in the database. The users money field that should be 5000 less stays exactly the same. Any suggestions ??? Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/#findComment-567368 Share on other sites More sharing options...
Mattyspatty Posted June 17, 2008 Share Posted June 17, 2008 $sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'"); $new = $sql - 5000; $update = mysql_query("UPDATE members SET money='$new' WHERE username='$username'"); $sql will be a resourceid. you need to do a mysql_fetch_array or mysql_fetch_rows first to get the actual value of money Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/#findComment-567377 Share on other sites More sharing options...
rhodesa Posted June 17, 2008 Share Posted June 17, 2008 silly me...i can't believe i missed this... $sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'"); the above returns a mysql resource...not the actual value from the money field...you still have to select that from the resource. here is some updated code...although i still don't understand some parts of it (see my comments in the code) <?php if($a == 'feed') { $username = $_GET['$username']; //Updated to be $username $date = date("y-m-d"); $check = mysql_query("SELECT * FROM `pets` WHERE `user`='$username'") or die("MySQL Error: ".mysql_error()); if(!mysql_num_rows($check)){ //Shortened this up a bit print "Record could not be found"; }else{ $sql = mysql_query("SELECT `money` FROM `members` WHERE `username`='$username'") or die("MySQL Error: ".mysql_error()); $member = mysql_fetch_assoc($sql); if(!$member){ print "Could not find member"; }else{ //Update member's money $new = $member['money'] - 5000; $update = mysql_query("UPDATE `members` SET `money`='$new' WHERE `username`='$username'"); $new2 = $MONEY + 5000; //WHERE is $MONEY coming from??? //WHERE is $u coming from??? $insert=mysql_query("INSERT INTO `petsfed` SET `user`='$u', `fed` = '$username', `date` = '$date'") or die("MySQL Error: ".mysql_error()); $update2=mysql_query("UPDATE `members` SET `money`='$new2' WHERE `username`='$u'") or die("MySQL Error: ".mysql_error()); $insert2=mysql_query("UPDATE `pets` SET `lastfed`='$date' WHERE `user`='$username'") or die("MySQL Error: ".mysql_error()); if($insert && $insert2 && $update && $update2) { printf ("Pets Fed: %d\n", mysql_affected_rows()); } } } } ?> p.s. - try to start indenting stuff, it makes it WAY easier to read Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/#findComment-567381 Share on other sites More sharing options...
fenway Posted June 17, 2008 Share Posted June 17, 2008 Seems like you could combine some of these. Quote Link to comment https://forums.phpfreaks.com/topic/109906-update-statement-not-updating/#findComment-567447 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.