Adding to an existing value. (integer)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Snaked
    New Member
    • Mar 2008
    • 11

    Adding to an existing value. (integer)

    [PHP]<?php

    //this function is the responsible of modifying the player in the table
    function modify_table()
    {
    //first the query try to get a row where the name of the player is = to the name in the url
    $query = "SELECT name FROM hi WHERE name='".$_GET["name"]."'";
    $res = mysql_query($qu ery) or die("Couldn't execute $query: ".mysql_error() );

    //if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
    if (mysql_num_rows ($res)==0)
    {
    //the query makes a new row with the player name and the score
    $query = "INSERT INTO hi VALUES('".$_GET["name"]."',".$_GET["score"].")";
    mysql_query($qu ery) or die("Couldn't execute $query: ".mysql_error() );
    }
    else
    {
    //the query looks for the row with name=name in the url and updates his score
    $query = "UPDATE hi SET score=".$_GET["score"]." WHERE name='".$_GET["name"]."'";
    mysql_query($qu ery) or die("Couldn't execute $query: ".mysql_error() );
    }
    }

    ?>[/PHP]

    This is a snippet of my code. When i send in a request to update a player's score, it overwrites the score already there with the new value. What i want it to do is ADD to the old score (eg. old score= 100 submitted score= 50 new score =150

    How would i go about this?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Have a look at this:
    [php]
    $_result = # mysql_query("SE LECT ... ");
    $_new_score = # get the new score to be added
    while($_row = mysql_fetch_arr ay($_result))
    {
    $_old_score = $_row['score']; # gets the old score
    $_updated_score = $_old_score + $_new_score; # add them together
    mysql_query("UP DATE ... ") or die("..."); // update here with updated_score.
    }
    [/php]
    Any questions?

    Regards

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Your first query must also select the score[php]$query = "SELECT name, score FROM hi WHERE name='".$_GET["name"]."'";
      $row=mysql_fetc h_assoc($res)
      or die ("blahblah.. ");
      [/php]
      You just add the new score to it and insert[php]
      $val=$row['score']+$_GET['score'];
      $query = "UPDATE hi SET score=$val WHERE name='".$_GET["name"]."'";[/php]P.S> Your page, when you run it as you show here, is hacker's heaven!

      Ronald

      Comment

      • Snaked
        New Member
        • Mar 2008
        • 11

        #4
        Thank you for the answer, my code has many encryptions elsewhere, that was just a snippet. edit- is this a direct code? can i copy/paste? or do i need to adapt it? i don't mind either way...

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by Snaked
          Thank you for the answer, my code has many encryptions elsewhere, that was just a snippet. edit- is this a direct code? can i copy/paste? or do i need to adapt it? i don't mind either way...
          Depends who you're asking?

          Comment

          • Snaked
            New Member
            • Mar 2008
            • 11

            #6
            i would be talking to roverdonk thank you... i tried using it directly, but it didn't work

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by Snaked
              i would be talking to roverdonk thank you... i tried using it directly, but it didn't work
              Then nuse this[php]<?php
              //this function is the responsible of modifying the player in the table
              function modify_table()
              {
              //first the query try to get a row where the name of the player is = to the name in the url
              $query = "SELECT name, score FROM hi WHERE name='".$_GET["name"]."'";
              $res = mysql_query($qu ery)
              or die("Couldn't execute $query: ".mysql_error() );

              //if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
              if (mysql_num_rows ($res)==0)
              {
              //the query makes a new row with the player name and the score
              $query = "INSERT INTO hi VALUES('".$_GET["name"]."',".$_GET["score"].")";
              mysql_query($qu ery) or die("Couldn't execute $query: ".mysql_error() );
              }
              else
              {
              $row=mysql_fetc h_assoc($res) ;
              $val=$row['score']+$_GET['score'];
              //the query looks for the row with name=name in the url and updates his score
              $query = "UPDATE hi SET score=$val WHERE name='".$_GET["name"]."'";
              mysql_query($qu ery) or die("Couldn't execute $query: ".mysql_error() );
              }
              }
              ?>[/php]Ronald

              Comment

              • Snaked
                New Member
                • Mar 2008
                • 11

                #8
                Thank you roverdonk! This question is solved.

                Comment

                • Snaked
                  New Member
                  • Mar 2008
                  • 11

                  #9
                  Oh, and by the way, the php action for modify_table is sent silently by an exe made by me

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by Snaked
                    Thank you roverdonk! This question is solved.
                    Ok, then see you next time here again.

                    Ronald

                    Comment

                    Working...