xcandiottix Posted September 11, 2010 Share Posted September 11, 2010 I have a function that contains 2 UPDATE commands. One updates a table whenever the function is ran. The second updates only if a condition is met. When the page loads the function is ran. For some reason the table is updated twice. So my table looks like this: Initially Viewed: 0 Replies: 0 Navigate to page: Viewed:2 Replies:0 Enter a reply and submit: Veiwed:4 Replies:1 No matter where I move the "viewed +1" code, it fires twice per page impression. Is this a glitch? Do functions run twice for some reason? If so why doesn't "reply +1" fire twice? //page loads as forum.php?goto=innertopic function connectForum(){ //connects DB if($_GET['goto'] == "innertopic"){ detailTopic(); } } function detailTopic(){ if(isset($_POST['content']{ //if there is a reply to a post mysql_query("UPDATE topics SET Replies=Replies+1") //works fine } else{ //no reply, just show all posts mysql_query("UPDATE topics SET Views=Views+1") //fires 2 times per page load .. ?!?!? //show posts //show reply window, posts to forum.php?goto=innertopic&&content=whateverTheUserTyped } } //html of page if($_GET['goto'] == "innertopic"){ connectForum(); } Link to comment https://forums.phpfreaks.com/topic/213174-weird-update-problem/ Share on other sites More sharing options...
GKWelding Posted September 13, 2010 Share Posted September 13, 2010 for a start, if this isn't just a copy paste error, then this function is wrong... function detailTopic(){ if(isset($_POST['content']{ //if there is a reply to a post mysql_query("UPDATE topics SET Replies=Replies+1") //works fine } else{ //no reply, just show all posts mysql_query("UPDATE topics SET Views=Views+1") //fires 2 times per page load .. ?!?!? //show posts //show reply window, posts to forum.php?goto=innertopic&&content=whateverTheUserTyped } } it should be function detailTopic(){ if(isset($_POST['content'])){ //if there is a reply to a post mysql_query("UPDATE topics SET Replies=Replies+1") //works fine } else{ //no reply, just show all posts mysql_query("UPDATE topics SET Views=Views+1") //fires 2 times per page load .. ?!?!? //show posts //show reply window, posts to forum.php?goto=innertopic&&content=whateverTheUserTyped } } Link to comment https://forums.phpfreaks.com/topic/213174-weird-update-problem/#findComment-1110530 Share on other sites More sharing options...
rwwd Posted September 13, 2010 Share Posted September 13, 2010 Hi there, Do this instead! :- function detailTopic(){ if(isset($_POST['content']) && !empty($_POST['content'])){ //added extra condition to the if clause mysql_query(" `UPDATE topics` SET `Replies` = 'Replies+1' "); //missed semi colon exit;//precautionary } else{ //no reply, just show all posts mysql_query("UPDATE `topics` SET `Views` = 'Views+1' "); //missing the semi colon exit;//just precautionary //show posts //show reply window, posts to forum.php?goto=innertopic&&content=whateverTheUserTyped } } Try that, it now reads better and will only work one time ;-p Cheers, Rw Link to comment https://forums.phpfreaks.com/topic/213174-weird-update-problem/#findComment-1110549 Share on other sites More sharing options...
xcandiottix Posted September 14, 2010 Author Share Posted September 14, 2010 Still adds 2. I ended up having to do this: if(!isset($_POST['content']) && empty($_POST['content'])){ $data = mysql_query("SELECT * FROM topic_table WHERE ForumName = '$forumname' AND ForumThread = '$threadname' AND ForumTopics='$forumtopic' ORDER BY Id DESC LIMIT 1") or die(mysql_error()); $info = mysql_fetch_array($data); $views = $info['Views']; $views = $views+1; mysql_query("UPDATE `topic_table` SET `Views` = $views WHERE ForumName='$forumname' AND ForumThread='$threadname' AND ForumTopics='$forumtopic'"); exit; } Insane. Link to comment https://forums.phpfreaks.com/topic/213174-weird-update-problem/#findComment-1110820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.