Jump to content

Prevent double post


fortnox007

Recommended Posts

Hi All,

 

I was wondering if someone maybe knows a nice way to prevent double posting or posting within a certain time without using javascript. Or maybe even echoing an error if someone posts the exact same ase the previous post.

 

I found this little snippet:

onClick="disabled=true;this.form.submit();return true;"

Which prevents double clicking.

But its javascript and I rather have something besides that to cover all situations. Would love to hear what you guys use or reccomend.

 

Thanks!

 

 

Link to comment
Share on other sites

To prevent a user fromposting the exact same content just do a simple query such as

SELECT id FROM posts WHERE userID = '$userID' AND posttext='$posttext'

 

Of course a user may post the exact same content legitimately, such as a canned response for a common question.

 

Another solution people use is to implement a wait period before a user can post another message. In that case you would just query for the timestamp of the most recent post by the user. If not enough time has elapsed don't add the post and give the user an appropriate message.

Link to comment
Share on other sites

Hey mjdamato,

 

Thanks for your swift response. Just to make it clear for myself (i am still in the noobie fase). Is it true that the query you gave will result in True, when an exact post has allready been posted and False if it didnt? And if that's the case use this result in an if-statement to echo out an error of somekind? Not sure how to do this ;)

Link to comment
Share on other sites

Queries do not return true/false. (Well, the result is false if the query fails, but that is not what you would normally expect). Queries return record sets. The "sample" query above would return a record set of all the records that match the WHERE criteria. Namely, and posts that have the same user ID and the same post content. You would then check the result set to see if the count is >1 (the user has submitted posts with the exact same content) or 0 (the user has never submitted a post with that exact same content).

 

So, you would need to check the count of the results and react accordingly.

Link to comment
Share on other sites

So this would not be the correct way?

 

<?php
$dbc = mysqli_connect('localhost','user','pass','database');

$query = "SELECT id FROM posts WHERE userID = '$userID' AND posttext='$posttext'";
$data = mysqli_query($dbc,$query);

while ($row = mysqli_fetch_array($data)){
   if (empty($row['id'])){
       echo 'Double post';
   }else{
       echo 'not a double post';
   }
}

?>

 

This is what I found at php.net maybe thats the way to go. I modified it a bit

 

<?php

$dbc = mysqli_connect('localhost','user','pass','database');

$query = "SELECT id FROM posts WHERE userID = '$userID' AND posttext='$posttext'";
$data = mysqli_query($dbc,$query);
$num_rows = mysql_num_rows($data);

if ($num_rows > 1){
  echo 'double post';
}else{
  echo 'not a double post';
}

?>

 

Link to comment
Share on other sites

The second code is closer to what you want. Remember, that code would flag a post as a duplicate even if the first one was made years in the past. If you want to allow someone to post the same thing after a certain amount of time you would want to add an additional parameter to the WHERE clause to find matches that are "recent", i.e. posts that have been created in the last day, week, whatever.

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.