Jump to content

Weird UPDATE problem


xcandiottix

Recommended Posts

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?

 

:confused:

//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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.