CAPTCHA and Database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

CAPTCHA and Database

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

You have used "firstname" in the form

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

Therefore you are inserting an empty variable!
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post 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:
Post Reply