PDA

View Full Version : MySQL/PHP Multiple Auto-increment (should not be multiple!)



zenthoef
22 Sep 2010, 10:31 PM
I have never used the INSERT or UPDATE commands in MySQL until now. So needless to say, I am writing this post because I am having a problem.

I am calling the following function only once on certain pages:


<?php
if(isset($_GET['id'])){
$sys->addPlay($_GET['id']);
}
?>

and that function is defined as:


function addPlay($gid){
$quickcheck = "SELECT GamesPlayed FROM gamestoday WHERE Date = CURDATE() AND gId=$gid";
if(@mysql_num_rows(mysql_query($quickcheck)) == 0){
@mysql_query("INSERT INTO gamestoday VALUES (CURDATE(),0,$gid)");
}
$update2 = "UPDATE gamestoday SET GamesPlayed=GamesPlayed+1 WHERE Date = CURDATE() AND gId=$gid";

@mysql_query($update2);

}


I believe if this function is called only once that the Auto-Increment in $update2 should increase GamesPlayed only once for a given date and "gId", correct? I am seeing behavior where if I load a page once (and I'm sure I'm the only one loading it at that instant) GamesPlayed will increment anywhere between 1 and 4. So if I'm at GamesPlayed=5 and I load a given page the GamesPlayed for that page could be anywhere between 6 and 9. What gives? Am I doing something wrong in my code or overlooking something else?

Thanks!