Jump to content

double submitting


roice
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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???)

Link to comment
Share on other sites

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;}  ?>" />

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.