Jump to content

Why is it saying that this variable is set?


aeroswat

Recommended Posts

Here is a little snippet of code that this problem pertains to.

 

	function clean($str) {
	$str = @trim($str);
	$str = str_replace('"',"", strip_tags($str));
	$str = str_replace("'","", strip_tags($str));
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}
if(isset($_POST['FName'])) {$studentFName = clean($_POST['FName']);}
$qry.= (isset($studentFName) ? "StudentFName LIKE '%" . $studentFName . "%' AND " : '');

 

so basically the previous form has a text box named FName. When I submit the form I leave FName unfilled. When it gets to the next page the $qry string has the "StudentFName LIKE '%%' AND" string included in it. If I'm right this shouldn't happen because FName was not set in the form. Am I missing something?

Except for check-boxes and radio-buttons, all form fields defined in a form are set, even if they are empty. Try the empty function instead. You would typically use isset to test if the form itself was submitted by checking if the submit button's name is set.

Try if( !empty($student_FName']) ) instead. If I'm not mistaken a text field in a submitted form will be set, but with an empty string as the value.

 

 

EDIT: Changed to show correct variable . . .  (Duh)

The field is set even when you leave it emtpy. If you do a

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

at the start of your script, you will see that.

 

What you need to do is to check that the field actually contains something:

<?php
if(isset($_POST['FName']) && strlen(trim($_POST['FName'])) > 0) {
     $studentFName = clean($_POST['FName']);
     $qry.= "StudentFName LIKE '%" . $studentFName . "%' AND ";
}
?>

 

BTW, using the empty function to test can return a false positive if the field contains the number zero ("0").

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.