Forms allowing Blank entries

Discussion in 'PHP' started by entri3, Aug 14, 2008.

  1. #1
    Hey i just started to learn php and i have a new user form and its allowing blank username , pass, and email help please
    	if (isset($_POST['submit'])) { // Form has been submitted.
    		$errors = array();
    
    		// perform validations on the form data
    		$required_fields = array('username', 'password','email');
    		$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
    
    		$fields_with_lengths = array('username' => 30, 'password' => 30,'email' =>30);
    		$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
    
    		$username = trim(mysql_prep($_POST['username']));
    		$password = trim(mysql_prep($_POST['password']));
    		$hashed_password = sha1($password);
    		$email    = trim (mysql_prep($_POST['email']));
    		
    		if ( empty($errors) ) {
    			$query = "INSERT INTO ausers (
    							username, hashed_password ,email
    						) VALUES (
    							'{$username}', '{$hashed_password}', '{$email}'
    						)";
    			$result = mysql_query($query, $connection);
    			if ($result) {
    				$message = "Welcome To JobLance $username" ;
    			} else {
    				$message = "The user could not be created.";
    				$message .= "<br />" . mysql_error();
    			}
    		} else {
    			if (count($errors) == 1) {
    				$message = "There was 1 error in the form.";
    			} else {
    				$message = "There were " . count($errors) . " errors in the form.";
    			}
    		}
    	} else { // Form has not been submitted.
    		$username = "";
    		$password = "";
    		$email    = "";
    	}
    ?>
    			<h2>Create New User</h2>
    			<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
    			<?php if (!empty($errors)) { display_errors($errors); } ?>
    			<form action="myfirst.php" method="post">
    PHP:

     
    entri3, Aug 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Add this:
    
    if (empty($username))
    {
        $errors[] = 'Enter a username';
    }
    
    PHP:
    ... right above this line:
    
    if ( empty($errors) ) {
    
    PHP:
     
    nico_swd, Aug 14, 2008 IP
  3. entri3

    entri3 Active Member

    Messages:
    286
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thanks Nico Quick Fix sorry im new to this
     
    entri3, Aug 14, 2008 IP