roice Posted June 10, 2012 Share Posted June 10, 2012 Hello, From time to time I found duplicated records (rows) in my database. Its looks like the user double click on the submit button. recently its happen more often so I add code that check when the last submit happened (I save the linux time) and check if there is 10 second delay: $query = mysql_query("SELECT create_date FROM `tkts_topics` WHERE (client = '$client') AND (email = '$email') AND (title = '$title') AND ($date - create_date <= 10) LIMIT 0,1"); if (mysql_num_rows($query)) { } I don't know why but its not working. is there any better way to prevent double submiting? Roi. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/ Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 Try this as your query. $query = mysql_query("SELECT COUNT(*) AS `exists` FROM `tkts_topics` WHERE `client` = '$client' AND `email` = '$email' AND `title` = '$title' AND `create_date` >= NOW() - INTERVAL 1 HOUR LIMIT 1"); PS - You should also disable your button onclick with javascript to prevent double-submission at the client side. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352628 Share on other sites More sharing options...
floridaflatlander Posted June 10, 2012 Share Posted June 10, 2012 Hello, From time to time I found duplicated records (rows) in my database. Its looks like the user double click on the submit button. recently its happen more often so I add code that check when the last submit happened (I save the linux time) and check if there is 10 second delay: $query = mysql_query("SELECT create_date FROM `tkts_topics` WHERE (client = '$client') AND (email = '$email') AND (title = '$title') AND ($date - create_date <= 10) LIMIT 0,1"); if (mysql_num_rows($query)) { } I don't know why but its not working. is there any better way to prevent double submiting? Roi. Okay so all you're doing is checking for duplicates right? Why do you need to use date in your select, can user name or email and title be enough? Unless I'm wrong it just adds stuff. Also I'd use either client or email but not both since a member's name and email are both unique(I would think). So I'd try $query = "SELECT title FROM tkts_topics WHERE email = '$email' AND title = '$title' "; $r = mysqli_query($dbc, $query); Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352631 Share on other sites More sharing options...
roice Posted June 10, 2012 Author Share Posted June 10, 2012 Hi smoseley, I'm not familiar with: - INTERVAL 1 HOUR What does it means? also, why did you wrote: AS `exists` ? what will it give? Do you have the code for the client side that you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352632 Share on other sites More sharing options...
roice Posted June 10, 2012 Author Share Posted June 10, 2012 floridaflatlander - some clients use the same email for all their department. Is it ok for 2 users with the same email to submit topics at the same time. I just don't want that each one of them will submit the same topice twice... Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352633 Share on other sites More sharing options...
floridaflatlander Posted June 10, 2012 Share Posted June 10, 2012 floridaflatlander - some clients use the same email for all their department. Is it ok for 2 users with the same email to submit topics at the same time. I just don't want that each one of them will submit the same topice twice... If that's the case you have to do what ever is unigue. name/title etc. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352635 Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 Client button disabling: <form action="whatever" onsubmit="document.getElementById('mybutton').disabled = true"> <input type="submit" id="mybutton" value="Submit" /> </form> In SQL: (NOW() - INTERVAL 1 HOUR) translates to one hour ago. Assuming your 'created' timestamp is populated by the database (DEFAULT CURRENT_TIMESTAMP), using a DB-generated time will ensure you're using a proper relative time, whereas a php time may be out of synch (if on different servers, for example). Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352642 Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 If that's the case you have to do what ever is unigue. name/title etc. In the case of a "topics" table (as per his example), there's likely no such thing as data uniqueness. Think of forum posts here... there could always be two identical posts by the same person years apart. The proper solution is to check time as well. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352643 Share on other sites More sharing options...
ZulfadlyAshBurn Posted June 10, 2012 Share Posted June 10, 2012 There are many ways to prevent a user from submitting a topic twice. But from what I see that you're trying to do is that you want to prevent the user from accidentally submitting it twice. The easiest is to set an ID to each form/topic submitted. When checking with DB if the topic is available, deny request to submit. If the ID is no in the DB, the allow the submitting to process. PS to smoseley: A gentle reminder that every code have to be place in a code tag. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352648 Share on other sites More sharing options...
roice Posted June 10, 2012 Author Share Posted June 10, 2012 smoseley - your code check if the user submit the same topic in the last hour (- INTERVAL 1 HOUR ), right? Can you right it so the check will be for the last 10 seconds? Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352651 Share on other sites More sharing options...
floridaflatlander Posted June 10, 2012 Share Posted June 10, 2012 Think of forum posts here... I see Can you right it so the check will be for the last 10 seconds? If they are double clicking it and I think they are I bet the odds are you'll need less than 10 seconds, you may need less than one second. You may need something like this PS - You should also disable your button onclick with javascript to prevent double-submission at the client side Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352658 Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 Try INTERVAL 1 MINUTE 10 seconds is not enough time to prevent a double-submit. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352663 Share on other sites More sharing options...
roice Posted June 10, 2012 Author Share Posted June 10, 2012 OK, I'll Thanks What do u think about using session for that - when user submit I open sesion the current time or even better - cookie that will destroies after 10 second when submit again I check if he have cookie. if yes this means that 10 seconds haven't been pass yet and he can't submit the form... that way I'm not using SQL queries... good idea? (If I'm, already login why do I need to write the capche every post???) Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352665 Share on other sites More sharing options...
floridaflatlander Posted June 10, 2012 Share Posted June 10, 2012 10 seconds is not enough time to prevent a double-submit. Or even one second Or you can try if (isset($_POST['submit'])) { // Check for a name: if (empty($_POST['name'])) { $error [] = 1; $name_e = '<h5 style="color: red;">* You forgot to enter a name.</h5>'; } else { $name = $_POST ['name']; $name = mysqli_real_escape_string($dbc, trim($name)); } // The other $_POST if (!$error) { // Run UPDATE query if ($results){ // Or mysqli_affected_rows might be better $name_submitted = ''; // Or any variable that would make sense. } } } <input type="text" name="name" size="90" value="<?php if (!empty($name_submitted)) {echo $name_submitted;} elseif (!empty($name)) {echo $name;} ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352667 Share on other sites More sharing options...
smoseley Posted June 10, 2012 Share Posted June 10, 2012 No sessions, no cookies, just implement the 2 checks I suggested - one in the <form> and one at the DB Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352676 Share on other sites More sharing options...
Yesideez Posted June 10, 2012 Share Posted June 10, 2012 You can use Javascript to hide the submit button as soon as it is clicked. Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/263937-double-submitting/#findComment-1352691 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.