Input to mysql multiple times

Hi,

I’ve created a form in php and when some people save the result to a mysql database sometimes it saves multiple times but most of the time it only saves one time, as it should.

I wonder what to do?

$db_host = 'xxx.xxxx.xx';
	$db_user = 'xxxx';
	$db_pass = 'xxxxx';
	$db_name = 'xxxxx';
	$db_table = 'xxxxx';

	$link = mysql_connect($db_host,$db_user,$db_pass);
	if(!$link) die ('Could not connect to database: '.mysql_error());
	mysql_query("SET NAMES utf8");
	mysql_select_db($db_name,$link);
	
		
	$query = "INSERT into `".$db_table."` (userid,name,requestid,question,motivation,email) VALUES ('" . $tavlande->id . "','" . $tavlande->name . "','0','". $radiosvar . "','". mysql_real_escape_string($bidrag) . "','". $tavlande->email . "')";

	$result = mysql_query($query);
	if (!$result) {
	//echo "output=error";
   		die('Invalid query: ' . mysql_error());
	}
	else { //echo "output=sent";
	}
	
	mysql_close($link);

Have you tried yourself? Maybe it is saving cause the person is clicking the button many times.

Welcome to the SP forums.

The code you posted saves only once. So multiple inserts means the code has to be ran multiple times. Maybe like Ryan says they hit the button more than once. Or maybe you do a redirect after the insert, and they hit the back button and it’s inserted again?

Thank You for your answere!

Yes I think Ryan is right because the loading time is sometimes slow. Do you have a nice tip on how to get around this problem?

Some may say you could use javascript to disable the button after clicking.

You would want it to disable the button via JS (which also helps with the UX since they received feedback that it submitted), as well as, on insert - check and see if the data already exists (since the data going in is identical - because JS can’t be trusted)