lip9000 Posted March 28, 2008 Share Posted March 28, 2008 I have a script that updates the 'views' field in a database, as I want it to increase by 1 when a person loads the page. The script goes as follows, and is at the very bottom of the page: $updateSQL = sprintf("UPDATE tblvideos SET views=views + 1 WHERE videoID=%s", GetSQLValueString($video, "int")); mysql_select_db($database_conn, $conn); $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error()); The script works, however when I refresh the page, the views increases by 2 instead of 1. I have tried changing "views + 1" to "views + 0" but then the views just stays the same and doesn't change, as it should. I have this exact same script on another site, and the views do indeed increase by 1 instead of 2. I have also tried this script by getting the current value of "views" in a variable, then increasing that varible by 1, and inserting that value into the query, but get the same result, an increase by 2 instead of 1. $tempview = $row_rsVideosWatch['views']; $nextview = $tempview + 1; $updateSQL = sprintf("UPDATE tblvideos SET views=$nextview WHERE videoID=%s", GetSQLValueString($video, "int")); mysql_select_db($database_conn, $conn); $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error()); Any suggestions??? This is really weird and is causing me to pull my hair out!!! Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/ Share on other sites More sharing options...
ohdang888 Posted March 28, 2008 Share Posted March 28, 2008 $updateSQL = sprintf("UPDATE tblvideos SET views= (views + 1) WHERE videoID=%s", Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502864 Share on other sites More sharing options...
ansarka Posted March 28, 2008 Share Posted March 28, 2008 check anywhere in your code whether you are running mysql_query($updateSQL, $conn) two times ;) Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502865 Share on other sites More sharing options...
lip9000 Posted March 28, 2008 Author Share Posted March 28, 2008 $updateSQL = sprintf("UPDATE tblvideos SET views= (views + 1) WHERE videoID=%s", Nope, it's still incrementing by 2. Even if I do + 0 instead of plus 1, logically you'd think it would only increase by 1, if the current code is already increasing it by 2. But instead of doesn't add anything. And no there is only 1 mysql_query($updateSQL, $conn) Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502888 Share on other sites More sharing options...
ansarka Posted March 28, 2008 Share Posted March 28, 2008 plz echo $updateSQL what the query its printing copy that query and run it in mysql without any php code (using phpmyadmin or any other tools) Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502889 Share on other sites More sharing options...
lip9000 Posted March 28, 2008 Author Share Posted March 28, 2008 please echo $updateSQL what the query its printing copy that query and run it in mysql without any php code (using phpmyadmin or any other tools) it echoed out as UPDATE tblvideos SET views=(views + 1) WHERE videoID=14 I ran it in phpMyAdmin, and sure enough the correct record increased by 1. So it works in there, but not in the document where its being executed, so perhaps theres another bit of code somewhere thats stuffing something up?? Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502896 Share on other sites More sharing options...
lip9000 Posted March 28, 2008 Author Share Posted March 28, 2008 Ahhh found the problem guys, I had another php file included in there, and as soon as i commented the include out, it started incrementing by 1 now. Thanks anyway sorry for the trouble! Link to comment https://forums.phpfreaks.com/topic/98276-number-is-incrementing-by-2-instead-of-1/#findComment-502898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.