Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129933
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129936
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129939
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129942
Share on other sites

....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());

 

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129949
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129953
Share on other sites

....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.

Link to comment
https://forums.phpfreaks.com/topic/217667-update-though-php/#findComment-1129991
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.