...

View Full Version : Adding to int in database?



dniwebdesign
04-08-2004, 09: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.

raf
04-08-2004, 11:21 AM
update table set var=var+1

sidney
04-08-2004, 11:23 AM
yes just add 1 to the field as below

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

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

dniwebdesign
04-10-2004, 01:25 AM
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
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? :confused:

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

sidney
04-10-2004, 01: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();
}

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

sidney
04-10-2004, 02: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

dniwebdesign
04-10-2004, 02: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.

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

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

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

should be

SET gp=gp+1

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

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

the values start are like...


$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
}

dniwebdesign
04-10-2004, 05: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.

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

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

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

something like:


$homeplayerf=''.$hgfn{"$thezero$hp"}.'';

sidney
04-10-2004, 11:49 PM
$homeplayerf=$hgfn.$thezero.$hp;

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

sidney
04-11-2004, 12:23 AM
$homeplayerf=${"$hgfn$thezero$hp"};

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

Why is the below code getting an MySQL Syntax?



for($i=01;$i<=9;$i++)
{
$hmin="hmin";
$hpfn="hpfn"; $hpln="hpln";
$v="v"; $h="h";
$pm=${"$hmin$thezero$i"};
$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

sidney
04-11-2004, 01: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();

dniwebdesign
04-11-2004, 01: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.

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

try echoing the two values to see if there set

if they are try

$a=${"$hpfn$thezero$i"};
$b=${"$hpln$thezero$i"};

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

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

dniwebdesign
04-11-2004, 02: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.

sidney
04-11-2004, 02: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
}

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

dniwebdesign
04-11-2004, 04:04 AM
Arrg,

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"];

for($i=1;$i<=20;$i++)
{
$a=$fn{"$i"};
$b=$ln{"$i"};
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>";
}
else
{
echo "<p> <font face=Arial size=2><p class=\"bodylgbold\">Error Adding Player</p></font>";
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum