SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    Code:
    $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);

  2. #2
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Have you tried yourself? Maybe it is saving cause the person is clicking the button many times.
    Ryan B | My Blog | Twitter

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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?

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Some may say you could use javascript to disable the button after clicking.
    Ryan B | My Blog | Twitter

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bokis View Post
    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?
    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)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •