Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
MySQL/PHP Multiple Auto-increment (should not be multiple!)
Old 09-22-2010, 10:42 PM MySQL/PHP Multiple Auto-increment (should not be multiple!)
Novice Talker

Posts: 11
Trades: 0
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:

Code:
<?php
	if(isset($_GET['id'])){
		$sys->addPlay($_GET['id']);
	}
?>
and that function is defined as:

Code:
	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!
zenthoef is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-22-2010, 11:15 PM Re: MySQL/PHP Multiple Auto-increment (should not be multiple!)
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
That code is a disaster. You're running a SELECT, (sometimes an INSERT), and an UPDATE every time.

Try an INSERT.... UPDATE trigger.

This assumes that your primary key is set to gId + date ( PRIMARY KEY (gId, Date) )

If not, you'll have to adjust that.

PHP Code:
function addPlay($gid){
    
$sql "INSERT INTO gamestoday (Date, GamesPlayed, gId)
            VALUES (CURDATE(),1,
$gid)
            ON DUPLICATE KEY
            UPDATE GamesPlayed=GamesPlayed+1"
;
    @
mysql_query($sql);

</span>
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 09-22-2010 at 11:17 PM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 09-22-2010, 11:50 PM Re: MySQL/PHP Multiple Auto-increment (should not be multiple!)
Novice Talker

Posts: 11
Trades: 0
I agree it was a disaster and your code has both educated me and made my life simpler.

However, I still have the same problem... That both sets of code will increment GamesPlayed more than once on occasion. Could this be something with the browser requesting the page multiple times so that function gets called multiple times?

Thanks.
zenthoef is offline
Reply With Quote
View Public Profile
 
Old 09-27-2010, 11:37 AM Re: MySQL/PHP Multiple Auto-increment (should not be multiple!)
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Sounds like a browser thing. Alternatively, it may be someone else looking at your website. You should have some PHP code using a Session variable or cookie to track updates and only run them conditionally, which will also prevent abuse.
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Reply     « Reply to MySQL/PHP Multiple Auto-increment (should not be multiple!)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16174 seconds with 12 queries