fortnox007 Posted September 7, 2010 Share Posted September 7, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/ Share on other sites More sharing options...
Psycho Posted September 7, 2010 Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108324 Share on other sites More sharing options...
fortnox007 Posted September 7, 2010 Author Share Posted September 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108338 Share on other sites More sharing options...
Psycho Posted September 7, 2010 Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108343 Share on other sites More sharing options...
fortnox007 Posted September 7, 2010 Author Share Posted September 7, 2010 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108345 Share on other sites More sharing options...
Psycho Posted September 7, 2010 Share Posted September 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108368 Share on other sites More sharing options...
fortnox007 Posted September 7, 2010 Author Share Posted September 7, 2010 Great thanks alot for you help m8 i really appreciate it : ) I learning every day. ty Quote Link to comment https://forums.phpfreaks.com/topic/212781-prevent-double-post/#findComment-1108380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.