tags:

views:

97

answers:

3

When I am executing following query then its not updating views column by 1 instead sometimes its updating it by 2 or 3. Say currently views count is 24 then after executing this query it becomes 26 or sometimes its 27.

$views = $views + 1;
$_SQL = '';
$_SQL = 'UPDATE videos SET views = '.$views.' WHERE VideoId= "'.$videoid.'";';
@mysql_query($_SQL);

I am not getting why this is happening, am I missing something or the query is executing 2 times automatically? Please help me to figure out the issue.

Thanks

+2  A: 

Instead of storing the view count, just have the SQL query increment the views,

 $_SQL = 'UPDATE videos SET views = views + 1 WHERE VideoId= "'.$videoid.'";'
St. John Johnson
+1  A: 

Is it possibly within a loop? The query is only executing once with this code posted however is this code contained within a while/for loop? If it is check it and then just move the

$views = $views + 1;

outside the scope of the loop.

Scott
A: 

You could try printing the value of $views after incrementing it just to see if the problem is in the PHP code or in the SQL query.

bogdanp9