View Full Version : Sending data twice...

03-30-2011, 03:46 AM
Guys, I have this script which sends data to a db and it has a captcha. The code is working perfect but, it keeps sending the same data into the db twice. Is there a way to modify to stop doing that? I can't see it. Thanks

$con = mysql_connect("localhost","root","root"); //Replace with your actual MySQL DB Username and Password
if (!$con)
die('Could not connect: ' . mysql_error());
// Where to redirect after form is processed.
$url = 'http://spanglish101.com';

mysql_select_db("", $con); //Replace with your MySQL DB Name

$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$genre_1=mysql_real_escape_string($_POST['genre_1']); //This value has to be the same as in the HTML form file

function is_valid_name($name) {
return ( $name);

if (!is_valid_name($name)) {
echo 'Sorry, you must enter your name. Please go back and enter your name';

function is_valid_email($e_mail) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $e_mail);

if (!is_valid_email($e_mail)) {
echo 'Sorry, you must enter an e-mail. Please go back and enter an e-mail';

$sql="INSERT INTO (name,e_mail,section,gender,age,genre_1,genre_2,ge nre_3,genre_4,genre_5,genre_6,time,feedback)

VALUES ('$name','$e_mail','$section','$gender','$age','$g enre_1','$genre_2','$genre_3','$genre_4','$genre_5 ','$genre_6','$time','$feedback')"; /*form_data is the name of the MySQL table where the form data will be saved.

name and email are the respective table fields*/

if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
echo "";

include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';

$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
// the code was incorrect
// you should handle the error so that the form processor doesn't continue

// or you can use the following code if there is no validation or you do not know how
echo "The security code entered was incorrect.<br /><br />";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
echo "Thank you for submitting.";
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


03-30-2011, 10:00 AM
The script must be getting called twice?

Its a very poorly written script and could be done with a lot less effort using a class

03-30-2011, 11:27 AM
O.K. do you have a better way to write it?

03-30-2011, 03:43 PM
I think it needs to be in a class

The idea is to create a class which holds functions which do your bidding separating your logic out from your design layer

I will look at it over the next day or so

post all your files including your html form

can you also export your database table in a SQL file to save me creating it

03-31-2011, 01:25 AM
look forward to that , let the classes begin.
re the script i was curious why the captcha was being validated after the insert statement that might be the root clause, but i advise you take david up on his offer

03-31-2011, 11:50 AM
E, you're right, I actually fixed it last nite. I had to change the captcha script from it's location and included it after it validated the e-mail. First it would validate the name, then the e-mail and then the captcha and now it works perfect. But if D wants to teach us something, I'm for it. Thanks