Forum Moderators: phranque
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?
<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.
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.