How to update/add number?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

How to update/add number?

Post by jmansa »

I have a database with 8 players ranking. (id, name, point) I the have a form where can insert points for each player whenever a match is done. Now i want to send the new data to the db and the add the new points to the points the allready have... e.g. if player 1 (id=1, name=john, point=11) should have earned 4 points, then when i submit my form his data should look like this (id=1, name=john, point=15).

I know how to insert data, and update but how do I add this together?

All respect...
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

get the existing value i.e 11, add 4 and save back the new value.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

UPDATE `players` SET `points` = `points` + $points WHERE `id` = $userID
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post by jmansa »

Looks like the right direction, but... I have tryid this but with error...
My form:

Code: Select all

<form id="form1" name="form1" method="post" action="index.php?page=oom_aprove">
<input name="arneid" type="hidden" id="arneid" value="1">
<input name="arnepla" type="text" size="5" class="form1"/>
<input name="arnepoint" type="text" size="5" value="0" class="form1"/>
<input name="boid" type="hidden" id="boid" value="2">
<input name="bopla" type="text" size="5" class="form1"/>
<input name="bopoint" type="text" size="5" value="0" class="form1"/>
<input type="submit" name="Submit" value="Submit">
Aprove script:

Code: Select all

$apuserid=$_POST['arneid'];
$bcuserid=$_POST['boid'];
$appoints=$_POST['arnepoint'];
$bcpoints=$_POST['bopoint'];

$query="UPDATE cms_orderofmerit_2007 SET Point = Point + $appoints WHERE ID = $apuserid";
mysql_query($query);

$query="UPDATE cms_orderofmerit_2007 SET Point = Point + $bcpoints WHERE ID = $bouserid";
mysql_query($query);

if(!mysql_query($query)){
print "Error Occurred:". mysql_error() ;
exit();
}
echo "Table updatet...";
mysql_close();
My error:
Error Occurred: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 '' at line 1
Can somebody see where I go wrong!?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Change this

Code: Select all

print "Error Occurred:". mysql_error() ;
to this

Code: Select all

print "Error Occurred:". mysql_error() ; 
echo $query;
Then post what it returns
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post by jmansa »

This is what it returns...
Error Occurred: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 '' at line 1
UPDATE cms_orderofmerit_2007 SET Point = Point + 5 WHERE ID =
It seems like it doesnt get the ID??? Why?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

where do you set $apuserid??
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post by jmansa »

In my form I have a hidden field like this:

Code: Select all

<input name="arneid" type="hidden" id="arneid" value="1">
Then in my aprove script i get it like this:

Code: Select all

$apuserid=$_POST['arneid'];
But actually it would be alot easier if I just inserted the ID number in forehand like this, but I the get an error?

Code: Select all

$query="UPDATE cms_orderofmerit_2007 SET Point = Point + $appoints WHERE ID = 1"; 
mysql_query($query);
How do I just staticly set the ID?
Post Reply