Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts

    incrementing a value in mysql

    ok, so incrementing a value in mysql is easy enough...but how would i increment a value on command? like if a user does something it increments it...like i connect to the database and like...++ it. kinda like in flat files where you do $i++ or something. i can't get it to work. would i like get the number from the database and then increment it with php then re put it? i need help...
    i have this code:

    PHP Code:
    // not really finished...i have no idea what i am doing.
    $get MYSQL_QUERY("SELECT points from users WHERE username='$username' and email='$email'");
    // matches username with email already saved in a  session(?)
    // how would i get a variable out of points from the database?
    $points $points++; // is this even possible?

    $resultMYSQL_QUERY("INSERT INTO users (points)".
    "VALUES ($points)"); 
    heh...well i tried huh?
    ..
    ▲ ▲

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    One query is all you need, an update query:
    Code:
    UPDATE MyTable
    SET points = points + 1
    WHERE ......

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:

    $result
    MYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'"); 

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow you beat me....

  • #5
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    0_0! wow thanks! i didn't know it was that simple. i am noobish when it comes to mysql, i bought some books that vaugely talk about mysql stuff such as this. yeah, i should get this up and running in a sec so i can test it. expect a lot of crap from me .
    ..
    ▲ ▲

  • #6
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ahhh its not working

    my code:
    PHP Code:
    <?
    session_start
    ();  
    header("Cache-control: private"); 

    $assume $_POST['assume'];
    $posterEMAIL $_POST['postemail'];
    $postTXT $_POST['posttxt'];
    $posterNAME $_POST['poster'];
    $postTITLE $_POST['posttitle'];


    function 
    filterBadWords($str){

     
    // words to filter
     
    $badwords=array( // removed due to badess 0_o );

     // replace filtered words with
     
    $replacements=array( "#@%*!""$%#*$!""@#$%*""*#!@#""#!~$%" );

     for(
    $i=0;$i sizeof($badwords);$i++){
      
    srand((double)microtime()*1000000); 
      
    $rand_key = (rand()%sizeof($replacements));
      
    $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str);
     }
     return 
    $str;
    }
    $postTXT filterBadWords($postTXT);
    $postTITLE filterBadWords($postTITLE);

    if (
    $assume == "true") {

    $dbcnx mysql_connect("*""*""*"); 
    mysql_select_db("flashanims_db");

        
         
    $sql "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', 
    postTXT='$postTXT', postTITLE='$postTITLE'"

         
    $resultMYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'");  
          if (
    mysql_query($sql)) { 
            echo(
    "

    Your comment has been added!
    "
    ); 
          } else { 
            echo(
    "

    Oh noes! there was an error adding entry: " 
    mysql_error() . "
    "
    ); 
          } 
        } 

    ?>
    the reason i am not trying to troubleshoot is that i have two different tables in the same db...should i be able to contact both tables in one thing? i don't know. so what i am trying to do is: when they post something...it adds a point to thier profile. easy enough i assume. i just don't know how i would go about doing this.

    edit: i have changed the type of "points" to decimal...does that mean it is a number? what do i need to do to make the "points" row a number that starts with zero and then as it gets incremented it goes up the scale?
    Last edited by Armondo; 03-25-2007 at 05:38 AM.
    ..
    ▲ ▲

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Where did you define $username and $email?

  • #8
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    in the session when you login. Does that code match the username and email together? i was wondering that...
    and so...what is happening in the code? it just doesn't increment.
    ..
    ▲ ▲

  • #9
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is my question to you.

    PHP Code:
     $sql "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', 
    postTXT='$postTXT', postTITLE='$postTITLE'"

         
    $resultMYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$username' and email='$email'"); 
    Is $posterNAME and $posterEMAIL supposed to be the same as $username and $email?

    IF so change your code like so.

    PHP Code:
     $sql "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', 
    postTXT='$postTXT', postTITLE='$postTITLE'"

         
    $resultMYSQL_QUERY("UPDATE users SET points = points + 1 WHERE username='$posterNAME' and email='$posterEMAIL'"); 

  • #10
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    i shall try it!

    edit: O M G...it works! thank you! and thanks also for helping my friend ryan. i gave you some rep .
    Last edited by Armondo; 03-25-2007 at 06:24 AM.
    ..
    ▲ ▲


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •