Page 1 of 1

CAPTCHA and Database

Posted: Fri Nov 24, 2006 11:16 am
by cdickson
I'm trying to integrate a CAPTCHA script into an existing form that updates a database.
The CAPTCHA works, and the auto email built into the form gets sent, but I can't get the database to update.

Code is:

Code: Select all

session_start();
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
    $firstname = trim(strip_tags($_POST['firstname']));
    $secure = strtoupper(trim(strip_tags($_POST['secure'])));
    $match = $_SESSION['captcha']; // the code on the image

// input error checking
    if ($firstname=="") {
        $err.= "Please provide your first name<br/>";
    }
if (!$secure) {
        $err.= "No security code entered<br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch<br/>";
    }
    if ($err=="") {
    // success - input passed all tests
	$query = "INSERT INTO memberlist(first_name) VALUES ('".$_POST['first_name']."')";
	
	//execute query
	if (@mysql_query ($query)) {
		echo 'Success message';
} else {
	
		echo 'Error message';
		
	}
	
	mysql_close();
	
   }
}


if ($err!="") {
    echo "<strong>Form Error(s)</strong><br/>";
    echo "<font color='#cc3300'>". nl2br($err). "</font><br/>";
}
?>

<form name="captcha" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table cellpadding="3" cellspacing="2" style="border:1px dotted #666;">
<tr>
<td>Name:</td><td><input type="text" name="firstname" value="<?php echo $_POST['firstname'];?>"/></td>
</tr>
<tr>
<td>Security Code</td><td><input type="text" name="secure"/></td>
</tr>
<tr>
<td><img src="captcha_image.php" alt="security image" border="0"/></td><td><input type="submit" name="submit" value="Send"/></td>
</tr>
</table>
</form>
Thanks for any help.

Posted: Tue Nov 28, 2006 5:17 pm
by andym01480
You have used "firstname" in the form

and "$_POST[first_name]" in the processing part.

Therefore you are inserting an empty variable!

Posted: Wed Nov 29, 2006 9:03 am
by cdickson
Ohmigosh that's one of the dumbest things I've ever seen.
Sorry to have even wasted your time, but appreciate the humbling! :oops: