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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts

    Question Adding to int in database?

    I don't suppose there is anyway to add a number to a value in a MySQL Database without knowing the inital value in the database?

    For example:

    My database has the value 23 in it...
    I want to be able to add say 1 to it so it is 24... I know how to do this, but can I do this without it knowing it is 23?

    (If that makes sense). I am making a script and to update all the info for one part I am going to need to use at least 60 query's to my database... (which is alot) so if I can do it without knowing the inital value in my database it will save on querying my database for it therefore cutting down the time... I hope.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    update table set var=var+1
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes just add 1 to the field as below

    mysql_query("update test set test2=test2+1 where id = '1'");

  • #4
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Or, if your only incrementing by 1, you could set it up as an auto-increment field.

  • #5
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts

    For Loop?

    I'm trying to update my database using a for loop. It seems to have my variable names right
    $hplayer01f
    $hplayer02f
    $hplayer03f

    and so on
    $player01l
    $player02l
    ...

    I have this
    PHP Code:
                echo "$"; echo "hplayer01f<br>";
                echo 
    "$hplayer01f<br>";
                for(
    $i=01;$i<=9;$i++)
                {    
    $v="v";
                    
    $h="h";    
                    
    $player="player";
                    
    $thezero="0";
                    
    $first="f";
                    
    $last="l";
                    
                
                echo 
    "$$h$player$thezero$i$first<br>";
                    
    $result=mysql_query("UPDATE hockeystats_players SET gp='gp+1' WHERE firstname='$$h$player$thezero$i$first' AND lastname='$$h$player$thezero$i$last'",$connection) or print mysql_error();
                } 
    How ever it doesn't seem to update my database? Is there any reason why?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Just to note that a few of those echos are for testing purposes....
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not sure if your trying to make a variable variable or a string for all those vars

    but try this for making a string

    $result=mysql_query("UPDATE hockeystats_players SET gp='gp+1' WHERE firstname='".$h.$player.$thezero.$i.$first."' AND lastname='".$h.$player.$thezero.$i.$last."'",$connection) or print mysql_error();
    }

  • #8
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Nope, still doesn't update the database...
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #9
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    does $hplayer01f hold a string value from somewhere else in the script

    and your trying to make a variable var in the loop

  • #10
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Yes the value does hold a string from a $_POST value and there are up to 30 for a home team and 30 for a visiting team. I figured using a for loop could cut down on my lines of code used.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #11
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this

    $result=mysql_query("UPDATE hockeystats_players SET gp='gp+1' WHERE firstname='".${"$h$player$thezero$i$first"}."' AND lastname='".${"$h$player$thezero$i$last"}."'",$connection) or print mysql_error();
    }

  • #12
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Still doesn't seem to update the database. My gp value is still 0.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #13
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    SET gp='gp+1'

    should be

    SET gp=gp+1

  • #14
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Excellent, thanks man... I'll probably be back before I'm done this thing...
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #15
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    845
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Yea, I need to do the same thing with an If Statement...

    the values start are like...
    PHP Code:
    $vtyp01=$_POST["vtyp01"];
    $htyp01=$_POST["htyp01"];
    $vtyp02=$_POST["vtyp02"];
    $htyp02=$_POST["htyp02"];

    $h="htyp"$v="vtyp";
    $pp=0$sh=0;
    for(
    $i=11;$i<=16;$i++)
    {
        if (
    $h$i=="SH")
        {
            
    $sh=1;
        }
        else if (
    $h$i=="PP")
        {
            
    $pp=1;
        }
        else
        {
        }

    //MY UPDATE QUERY GOES HERE

    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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