Sanjib Sinha Posted February 4, 2009 Share Posted February 4, 2009 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 https://forums.phpfreaks.com/topic/143732-solved-why-undefined-came/ Share on other sites More sharing options...
uniflare Posted February 4, 2009 Share Posted February 4, 2009 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 https://forums.phpfreaks.com/topic/143732-solved-why-undefined-came/#findComment-754148 Share on other sites More sharing options...
peranha Posted February 4, 2009 Share Posted February 4, 2009 <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 https://forums.phpfreaks.com/topic/143732-solved-why-undefined-came/#findComment-754149 Share on other sites More sharing options...
uniflare Posted February 4, 2009 Share Posted February 4, 2009 ya was hoping he'd figure that out Link to comment https://forums.phpfreaks.com/topic/143732-solved-why-undefined-came/#findComment-754151 Share on other sites More sharing options...
Sanjib Sinha Posted February 4, 2009 Author Share Posted February 4, 2009 Many Many Thanks. It is solved. It's a typo problem that I had overlooked. Link to comment https://forums.phpfreaks.com/topic/143732-solved-why-undefined-came/#findComment-754171 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.