Jump to content

[SOLVED] Why undefined came?


Sanjib Sinha

Recommended Posts

I have two pages new_user.php and another in my includes file form_functions.php

code of new_user.php is as follows:

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include_once("includes/form_functions.php");


// START FORM PROCESSING
if (isset($_POST['submit'])) { // form has been submitted.
        $errors = array();
        
        // perform validation on the form data
        $required_fields = array('username', 'password');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));    
    	 
        $fields_with_lengths = array('username' => 30, 'password' => 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);
    
    if(empty($errors)) {
	$query = "INSERT INTO users ( username, hashed_password ) VALUES ( '{$username}', '{$hashed_password}' )";
    $result = mysql_query($query);
    if (mysql_affected_rows() == 1) {
	// Success
	$message = "The user was successfully created. ";
} else {
	$message = "The user could not be created. ";
	$message .= "<br />" . mysql_error();
}
    } else {
	if (count($errors) == 1){
		$message = "There was one error in the form.";
	} else {
		$message = "There were " . count($errors) . " errors in the form.";
	}
}
// END FORM PROCESSING




} else { // form has not been submitted
$username = "";
$password = ""; 
} 

?>
<?php include("includes/header.php"); ?>

<table id="structure">
<tr>
<td id="navigation">
<a href="staff.php">Return to Menu</a><br />
<br />
</td>
<td id="page">
<h2>Create New User</h2>
<?php if (!empty($message)) { echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
<form action="new_user.php" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /></td>
</tr>
<tr>
<td>Pasword</td>
<td><input type="password" name="pasword" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Create user" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

 

And the form_functions.php code is like this:

 

<?php
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
	if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
	 $field_errors[] = $fieldname;
	}
}
return $field_errors;
}

function check_max_field_lengths($field_length_array){
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength){
	if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){	$field_errors[] = $fieldname; }
}
return $field_errors;
}


function display_errors($error_array){
echo "<p class=\"errors\">";
echo "Please review the following fields:<br />";
foreach($error_array as $error) {
	echo " - " . $error . "<br />";
}
echo "</p>";
}
?>

 

Now the output comes as

Notice: Undefined index: password in C:\wamp\www\Bengali_Friends\includes\form_functions.php on line 15

 

Notice: Undefined index: password in C:\wamp\www\Bengali_Friends\new_user.php on line 19

 

:)

Why this happens? Any idea from anyone?

 

 

 

Link to comment
Share on other sites

An index is a "Label" for an array item. eg;

 

$test = Array(
   "this_is_an_index" => "this is a value",
   1 => "another value, 1 is the index"
);

so, echo($test['this_is_an_index']); would echo ("this is a value");

echo($test[1]); // would echo ("another value, 1 is the index");,

as you can see $test['password'] would give you an "Undefined Index", or "Array item does not exist".

 

 

 

i believe $_POST['password'] doesnt exist, try using: (anywhere, it will show you the form data available to that page).

echo("<PRE>"); var_dump($_POST); echo("</PRE>");

 

 

Link to comment
Share on other sites

<input type="password" name="pasword" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>

 

should be

 

<input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>

 

your name only has 1 s in password, not 2

Link to comment
Share on other sites

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.