Forum Moderators: phranque

Message Too Old, No Replies

weird problem - Two DB Inserts for each click of "Submit"

         

keym

8:24 pm on Nov 14, 2006 (gmt 0)

10+ Year Member



I am experiencing a weird problem, perhaps someone might hazard a guess as to what could be going on...

I have an html form which takes an email address, and invokes a php script to write the email to an MySQL table.

Simple enough.

The problem is, for every click on "SUBMIT", two records are written to the table, not one.

This problem doesn't occur in my localhost PC development environment, only when I use the remote server. (browser is running on my local PC).

Any ideas what could be happening?

pixeltierra

1:56 am on Nov 15, 2006 (gmt 0)

10+ Year Member



Could you show the code that makes the insert statement?

keym

7:22 am on Nov 15, 2006 (gmt 0)

10+ Year Member



The Form:
-----------

<form name='subscribe' action='subscribe.php'>
<input type='text' name='email' size='20' value='<?php print (isset($email)?$email:"");?>' />
<input type='image' align="middle" src= <?php print "images/GoButton.gif";?> />
</form>

The action (subscribe.php):
---------------------------

<?php

// Get the email.

$email = (isset($_GET["email"])?($_GET["email"]):"");

// Insert into the subscribers table.

$sql = "INSERT INTO subscribers SET email = '".$email."', date = NOW()";
db_send($sql,$insert_id);

// redisplay the page (of which the form is a part).
require("html/page.php");

?>

Here is the db routine (db_send.php):
-------------------------------------

function db_send($sql,&$insert_id)
{
// some global defn's snipped out...

$conn = @mysql_connect($db_srvr,$db_uname,$db_passwd);
@mysql_select_db($db_name,$conn);
$query_result = mysql_query($sql,$conn);
$insert_id = mysql_insert_id();
return mysql_affected_rows();
}

Thanks for any help.

pixeltierra

6:29 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



It isn't obvious why it would be submitted twice. It is either that the command is going twice on one page load, or the command is going once and there are two page loads. Or it could be a weird error on the server. I always assume the error is mine, but that's not always the case.

I don't understand your use of $insert_id in your function db_send. I don't see why it's being sent (especially by reference). I would also do some validation on the email and use mysql_real_escape_string on input. Try re-directing to the form page instead of including it, that might do something. (You could also use a auto date field so you don't have to manually insert it. )

In funky cases, I tend to eliminate problems by reducing the situation. I would make a page that just has this and nothing else:

$qry = "NSERT INTO subscribers SET email = 'test@test.com', date = NOW()";
db_send($qry,$insert_id);

If it is submitted twice, then you know it has nothing to do with user input, variable poisoning, or your page logic.

carguy84

2:05 am on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd disable the form button after it's been clicked once to make sure it's not just users double clicking it.

Chip-