View Full Version : Adding to int in database?

04-08-2004, 10:16 AM
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.

04-08-2004, 12:21 PM
update table set var=var+1

04-08-2004, 12:23 PM
yes just add 1 to the field as below

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

04-08-2004, 01:51 PM
Or, if your only incrementing by 1, you could set it up as an auto-increment field.

04-10-2004, 02:25 AM
I'm trying to update my database using a for loop. It seems to have my variable names right

and so on

I have this
echo "$"; echo "hplayer01f<br>";
echo "$hplayer01f<br>";
{ $v="v";

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? :confused:

04-10-2004, 02:33 AM
Just to note that a few of those echos are for testing purposes....

04-10-2004, 02:38 AM
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();

04-10-2004, 02:51 AM
Nope, still doesn't update the database...

04-10-2004, 03:04 AM
does $hplayer01f hold a string value from somewhere else in the script

and your trying to make a variable var in the loop

04-10-2004, 03:15 AM
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.

04-10-2004, 03:20 AM
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();

04-10-2004, 03:26 AM
Still doesn't seem to update the database. My gp value is still 0.

04-10-2004, 03:29 AM
SET gp='gp+1'

should be

SET gp=gp+1

04-10-2004, 03:31 AM
Excellent, thanks man... I'll probably be back before I'm done this thing...

04-10-2004, 06:18 AM
Yea, I need to do the same thing with an If Statement...

the values start are like...


$h="htyp"; $v="vtyp";
$pp=0; $sh=0;
if ($h$i=="SH")
else if ($h$i=="PP")


04-10-2004, 06:27 AM
My update querys are pretty close to the same as before only another set is there for points. Which works good. just need to have a set part if it is a SH or PP goal and update the player stats for that.

04-10-2004, 07:10 AM
If anyone knows please post... I'm heading to bed at the moment and will check in the morning.

04-10-2004, 01:37 PM
if (${"$h$i"}=="SH")

04-11-2004, 12:16 AM
Hey man, sorry to bother you again about this but how would I get that to store into one variable?

something like:


04-11-2004, 12:49 AM

04-11-2004, 01:11 AM
Well that would store the value "hgfn03" I need it to display the value of "$hgfn03".

04-11-2004, 01:23 AM

04-11-2004, 02:22 AM
I might be blind, but I might not be...

Why is the below code getting an MySQL Syntax?

$hpfn="hpfn"; $hpln="hpln";
$v="v"; $h="h";
$result=mysql_query("UPDATE hockeystats_players SET pm=pm+$pm WHERE firstname='".${"$hpfn$thezero$i"}."' AND lastname='".${"$hpln$thezero$i"}."'",$connection) or print mysql_error();

And the error is:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE firstname='' AND lastname=''' at line 1

04-11-2004, 02:35 AM
$result=mysql_query("UPDATE hockeystats_players SET pm=pm+".$pm." WHERE firstname='".${"$hpfn$thezero$i"}."' AND lastname='".${"$hpln$thezero$i"}."'",$connection) or print mysql_error();

04-11-2004, 02:39 AM
I still have the error... Would it be because my values from 3-9 are emtpy? I never had that problem with the earler database updates for goals.

04-11-2004, 02:47 AM
echo ${"$hpfn$thezero$i"}."<br>".${"$hpln$thezero$i"};

try echoing the two values to see if there set

if they are try


mysql_query("UPDATE hockeystats_players SET pm=pm+".$pm." WHERE firstname='".$a."' AND lastname='".$b."'",$connection) or print mysql_error();

04-11-2004, 03:02 AM
They were set and so I changed it, it still updates the database but I still get those errors.

04-11-2004, 03:13 AM
I guess the reason is because my last entries were empty.

I just added an if loop saying if the field is empty don't do anything, if there is something, update the database. Thanks for your help either way.

04-11-2004, 03:16 AM
if its updating then the line is correct

and the 3-9 emtpy fields mustbecausing the error

try a
if(!empty($a) && !empty($b)){
around yourupdate query

04-11-2004, 03:17 AM
see you thought of it too

04-11-2004, 05:04 AM

Once again I require your help... I always keep getting the "Error Adding Players" and it doesn't update the players. Any fix? I don't think I screwed up...

$fn20=$_POST["pf20"]; $ln20=$_POST["pl20"]; $number20=$_POST["playnumber20"]; $position20=$_POST["position20"]; $team20=$_POST["playersteam20"];

if(!empty($a) && !empty($b))
$result=mysql_query("INSERT INTO hockeystats_players (number,team,firstname,lastname,position) VALUES ('".$number{"$i"}."','".$team{"$i"}."','".$fn{"$i"}."','".$ln{"$i"}."','".$position{"$i"}."')") or print mysql_error();
if ($result)
echo "<p class=\"bodymd\"><font face=Arial size=2>The player(s) has been added to the database.</font></p>";
echo "<p> <font face=Arial size=2><p class=\"bodylgbold\">Error Adding Player</p></font>";

04-11-2004, 06:09 AM
Nevermind, I got it to work.