Jump to content

checking for blank fields


fife

Recommended Posts

Hi.  I've been doing tutorials all day on checking for blank fields and I have wrote a function to do so.  For now I am only checking one field to see if it works.  If I miss that field out (first name)  It works great and displays the correct error message.  Problem I have is if I fill in the whole form and send it I get an Inturnal server error.  I will post my code to show you.  can anyone see whats going wrong?  I have \\    some of the code for now because I my keep it if I cant do this error check.

 

session_start();  
$validation_id = strval(time());





if(isset($_POST['submit'])) {




$first_name = check_input($_POST['first_name'],"Please enter a first name");
$last_name = check_input($_POST['last_name']);
   $DOB = check_input($_POST['DOB']);
    $sex = check_input($_POST['sex']);
 $email = check_input($_POST['email']);
  $username = check_input($_POST['username']);
   $password = check_input($_POST['password']); 
   $agree = check_input($_POST['agreed']);
    $creation_date = check_input($_POST['creation_date']);
	$user_type = check_input($_POST['member_type']);
	 $access_level = check_input($_POST['access_level']);
	  $validation = check_input($_POST['validation_id']);
	  	  $club_user =check_input($_POST['user_type']);


//    $first_name = mysql_real_escape_string($_POST['first_name']);
//  $last_name = mysql_real_escape_string($_POST['last_name']);
//   $DOB = mysql_real_escape_string($_POST['DOB']);
//   $sex = mysql_real_escape_string($_POST['sex']);
//	 $email = mysql_real_escape_string($_POST['email']);
//	  $username = mysql_real_escape_string($_POST['username']);
//	   $password = mysql_real_escape_string($_POST['password']); 
//	   $agree = mysql_real_escape_string($_POST['agreed']);
//	    $creation_date = mysql_real_escape_string($_POST['creation_date']);
//		$user_type = mysql_real_escape_string($_POST['member_type']);
//		 $access_level = mysql_real_escape_string($_POST['access_level']);
//		  $validation = mysql_real_escape_string($_POST['validation_id']);
//		  	  $club_user = mysql_real_escape_string($_POST['user_type']);
		  
$insert_member= "INSERT INTO Members (`first_name`,`last_name`,`DOB`,`sex`,`email`,`username`,`password`,`agree`,`creation_date`,`usertype`,`access_level`,`validationID`) 
VALUES 
('".$first_name."','".$last_name."','".$DOB."','".$sex."','".$email."','".$username."','".$password."','".$agree."','".$creation_date."','".$user_type."','".$access_level."', '".$validation."')";

$insert_member_now= mysql_query($insert_member) or die(mysql_error());

$url = "thankyou.php?name=".$_POST['username'];
	header('Location: '.$url);

and the form

<form method="POST" name="member_accounts" id="member_accounts">
<input name="first_name" type="text" class="form_fields" value="<?php echo $_POST['first_name'];?>" size="20" />
<input name="last_name" type="text" class="form_fields" value="<?php echo $_POST['last_name'];?>" size="20" />
<input name="submit" type="submit" class="join_submit" id="submit_member" value="Create Account" />


<?  function check_input($data, $problem='')
{
$data= trim($data);
$data= stripslashes($data);
$data= htmlspecialchars($data);
if ($problem && strlen($data) ==0)
{
die($problem);
}
return $data;

}   ?>

Link to comment
https://forums.phpfreaks.com/topic/209241-checking-for-blank-fields/
Share on other sites

Hi fife,

 

Well, if that's your full form, there's the problem right there. You only have three fields: first_name, last_name, and submit. Therefore, when you post the data, only 3 variables get stored:

 

$_POST['first_name']

$_POST['last_name']

$_POST['submit']

 

If you leave either of these blank, the check_input() catches it and causes the server to halt execution. Say, for example, you left the last_name field blank. Your code would first check to see that $_POST['submit'] is set, then it would go on to evaluate the result of check_input($_POST['first_name']). It would find that you have entered a first name. Then, it would get to check_input($_POST['last_name']), and would see that the field is empty, so the PHP would halt execution.

 

On the other hand, if check_input() finds that no fields are blank, it won't halt execution, so it goes on to evaluate check_input($_POST['DOB'])... but $_POST['DOB'] doesn't even exist! You can't pass a variable that doesn't exist. Finish creating your form, and then give it a try. If that doesn't work, then the problem lies somewhere else.

 

-jm

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.