dean7 Posted November 3, 2010 Share Posted November 3, 2010 Hi all, ive coded a script for my website which allowes users to do some missions to help them along there way, but the script worked well untill I needed to update a field of the database.. <?php session_start(); include ("includes/config.php"); include ("includes/functions.php"); logincheck(); ini_set ('display_errors', 1); error_reporting (E_ALL); $username = $_SESSION['username']; $userget = mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1") or die ("Error - Query : Line 7 : " . mysql_error()); $fetch = mysql_fetch_object($userget); $garageget = mysql_query("SELECT * FROM garage WHERE id='$fetch->carid' AND owner='$username' LIMIT 1") or die ("Error - Query : Line 12 : " . mysql_error()); $garage = mysql_fetch_object($garageget); if ($fetch->missions == "1"){ if ($fetch->missiononeraces == "25"){ echo ("<table width='30%' cellpadding='0' align='center' cellspacing='0' border='1' class='table'> <tr> <td class='header' align='center'>Well Done!</td> </tr> <tr> <td>You Successfully Completed Mission 1!</td> </tr> </table><br />"); $newmoney = $fetch->money + 5000000; $newrep = $fetch->rep + 75000; mysql_query ("UPDATE users SET money='$newmoney' WHERE username='$username'"); mysql_query ("UPDATE users SET rep='$newrep' WHERE username='$username'"); mysql_query ("UPDATE users SET missions='2' WHERE username='$username'") or die ("Error - Query : Line 27 : " . mysql_error()); mysql_query ("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` , `saved` , `event_id` ) VALUES ( '', '$username', 'System', 'Well done! <br /> As You completed Mission 1 Successfully we will reward you with<br /> £5,000,000 Money<br /> 75,000 Rep!', '$date', '0', '0', '0' )"); mysql_query("UPDATE users SET missiononeraces='0' WHERE username='$username'") or die ("Error - Query : Line 83 : " . mysql_error()); } } // Mission 1 ?> <html> <head> <title>Missions || SD</title> </head> <body> <form action="" method="POST" name="mission1"> <?php if($fetch->missions == "1"){ ?> <table width="50%" cellpadding="0" cellspacing="0" border="1" align="center" class="table"> <tr> <td class="header" align="center">Mission 1</td> </tr> <tr> <td>You must prove your self to the other Racers, that new racers arnt allways bad.<br /> To prove this win 25 Races!</td> </tr> <tr> <td align="center">You are on <?php echo "$fetch->missiononeraces!"; ?></td> </tr> </table> </form> <?php } ?> </body> </html> Once the user has done 25 races it is suppose to update missiononeraces in the database to 0, but how ive got it at the moment its just saying theve one 25 races and nothing happends, but if I remove the update query it will work. Anyone see why? Thanks for any help given Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/ Share on other sites More sharing options...
mikosiko Posted November 3, 2010 Share Posted November 3, 2010 my first comment for you... all this: mysql_query ("UPDATE users SET money='$newmoney' WHERE username='$username'"); mysql_query ("UPDATE users SET rep='$newrep' WHERE username='$username'"); mysql_query ("UPDATE users SET missions='2' WHERE username='$username'") or die ("Error - Query : Line 27 : " . mysql_error()); should be written in this way: mysql_query ("UPDATE users SET money='$newmoney' , rep='$newrep' , missions='2' WHERE username='$username'") or die ("Error - Query : Line 27 : " . mysql_error()); now... regarding to your "missiononeraces" ... where are you increasing it value to allow it to reach 25? also, what datatype are "missions" and "missiononeraces" in the table?... numbers or strings? Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129933 Share on other sites More sharing options...
dean7 Posted November 3, 2010 Author Share Posted November 3, 2010 my first comment for you... all this: mysql_query ("UPDATE users SET money='$newmoney' WHERE username='$username'"); mysql_query ("UPDATE users SET rep='$newrep' WHERE username='$username'"); mysql_query ("UPDATE users SET missions='2' WHERE username='$username'") or die ("Error - Query : Line 27 : " . mysql_error()); should be written in this way: mysql_query ("UPDATE users SET money='$newmoney' , rep='$newrep' , missions='2' WHERE username='$username'") or die ("Error - Query : Line 27 : " . mysql_error()); now... regarding to your "missiononeraces" ... where are you increasing it value to allow it to reach 25? also, what datatype are "missions" and "missiononeraces" in the table?... numbers or strings? Missions and missiononeraces are both Varchar in the table, but its a different file which increase's the number on missiononeraces, which isnt yet coded, but for the time being I was using the database to see if the script worked. Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129936 Share on other sites More sharing options...
BlueSkyIS Posted November 3, 2010 Share Posted November 3, 2010 I disagree that the SQL should be written like that. Having found it that way in existing code, I find it confusing and a hassle to modify. Although some people might prefer to write it that way, it is purely a style preference and therefore shouldn't be considered a rule. However, I definitely agree that every mysql_query() should be followed by or die(mysql_error()) and any other info you want to put into the die() statement. Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129939 Share on other sites More sharing options...
mikosiko Posted November 3, 2010 Share Posted November 3, 2010 I disagree that the SQL should be written like that. Having found it that way in existing code, I find it confusing and a hassle to modify. Although some people might prefer to write it that way, it is purely a style preference and therefore shouldn't be considered a rule.. WHAT?.... I think you missed the point completely... are you saying that is BETTER to write 3 UPDATES instead of JUST ONE... I don't think so... now... if you are referring to the way that I did indent it .. that is something totally different and as you said is just a matter of style and personal preferences and coding habits... so please next time clarify exactly what are you disagreeing to do not gave rest of users wrong ideas/examples. thanks Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129942 Share on other sites More sharing options...
mikosiko Posted November 3, 2010 Share Posted November 3, 2010 ....Missions and missiononeraces are both Varchar in the table, but its a different file which increase's the number on missiononeraces, which isnt yet coded, but for the time being I was using the database to see if the script worked. so please clarify for me how do you know that your UPDATE code (below) is not working ? mysql_query("UPDATE users SET missiononeraces='0' WHERE username='$username'") or die ("Error - Query : Line 83 : " . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129949 Share on other sites More sharing options...
BlueSkyIS Posted November 3, 2010 Share Posted November 3, 2010 I disagree that the SQL should be written like that. Having found it that way in existing code, I find it confusing and a hassle to modify. Although some people might prefer to write it that way, it is purely a style preference and therefore shouldn't be considered a rule.. WHAT?.... I think you missed the point completely... are you saying that is BETTER to write 3 UPDATES instead of JUST ONE... I don't think so... now... if you are referring to the way that I did indent it .. that is something totally different and as you said is just a matter of style and personal preferences and coding habits... so please next time clarify exactly what are you disagreeing to do not gave rest of users wrong ideas/examples. thanks I apologize. I did miss the point completely. Yes, there should only be one query instead of 3. However, you did not state that intent in your post. so please next time clarify exactly what are you correcting do not gave rest of users wrong ideas/examples. thanks Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129953 Share on other sites More sharing options...
dean7 Posted November 3, 2010 Author Share Posted November 3, 2010 ....Missions and missiononeraces are both Varchar in the table, but its a different file which increase's the number on missiononeraces, which isnt yet coded, but for the time being I was using the database to see if the script worked. so please clarify for me how do you know that your UPDATE code (below) is not working ? mysql_query("UPDATE users SET missiononeraces='0' WHERE username='$username'") or die ("Error - Query : Line 83 : " . mysql_error()); The query itself works if I put it where it is now it dont say the user has succeded the mission when they have. But if I move the query down to the bottom of the script it says that they succeed the mission but every time you go onto that page it resets the number of races won. Quote Link to comment https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129991 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.