phatmambo Posted November 26, 2010 Share Posted November 26, 2010 I know this problem comes up a lot but it can be for various reasons so after reading up I can't decide what might be wrong. I'm still learning! I have a simple php registration form (first name, second name, email address) and every time a user submits an entry a second blank record is created in the MYSQL database after it. Any help would be great. (php code in red) [color=red]<? $firstname=$_POST['firstname']; $surname=$_POST['surname']; $email=$_POST['email']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO register VALUES ('','$firstname','$surname','$email')"; mysql_query($query); mysql_close(); ?>[/color] Here is the section on the html HTML page that submits the form <form action="register.php" method="post" class="BagTitle"> <table width="700" border="0" cellpadding="0" cellspacing="2"> <tr> <td width="100">First Name: </td> <td width="594"><input name="firstname" type="text" size="50" /></td> </tr> <tr> <td>Last Name: </td> <td><input name="surname" type="text" size="50" /></td> </tr> <tr> <td>E-mail:</td> <td><input name="email" type="text" size="50" /></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2"><input type="Submit" value="Send" /> <input name="Reset" type="reset" value="Reset Form" /></td> </tr> </table> </form></p> Also I have no idea about securing this information. Are there any basic steps I can take? Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/ Share on other sites More sharing options...
revraz Posted November 26, 2010 Share Posted November 26, 2010 Only run the PHP code if Submit is set. Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139883 Share on other sites More sharing options...
phatmambo Posted November 26, 2010 Author Share Posted November 26, 2010 Hi thanks I guess I need to use something like: if (isset($_POST['submit'])) I'm a little unclear how or where though? Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139895 Share on other sites More sharing options...
phatmambo Posted November 26, 2010 Author Share Posted November 26, 2010 I've just discovered that it doesn't add the extra blank record in if the user is using internet explorer. I was testing it with Firefox (it definitely does add in a blank record after the new one. Does this help? I'm still a bit lost on how to fix this???? Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139934 Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2010 Share Posted November 26, 2010 Firefox will request a page twice due to using debugging add-ons and at least in the past it would request a page twice if the default character encoding you have set in the browser does not match the character encoding on the page. Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139936 Share on other sites More sharing options...
phatmambo Posted November 26, 2010 Author Share Posted November 26, 2010 I really have no idea how to fix this. I have seen other cases when searching around where just a couple of unnecessary <td> tags in the html have caused the browser to refresh and send the data twice - once properly and once with just blank fields. Does anyone have any ideas? Even if there isn't a way of stopping firefox refreshing, can I just say in the php code - don't send to the database if fields are empty or if button hasn't actually been clicked. This would then surely fix the problem??? Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139982 Share on other sites More sharing options...
PFMaBiSmAd Posted November 26, 2010 Share Posted November 26, 2010 Your code should be validating the form data and only performing the query when all the expected data have values. Doing this would filter out empty submissions. Quote Link to comment https://forums.phpfreaks.com/topic/219891-quick-fix-form-entering-blank-records-same-old-problem/#findComment-1139984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.