By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Undefined Index Error While Inputting A New User

P: 22
I'm creating a page where I will create an account for authorized users. But I always get into this Undefined Index error. There are only two variables, but I can't seem to find what is wrong? Could someone please check my code and see what is wrong? I've also linked some images. Sorry for my noobiness.






+++THE FORM_FUNCTIONS CODE+++

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. function check_required_fields($required_array) {
  4.     $field_errors = array();
  5.     foreach($required_array as $fieldname) {
  6.         if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) &&
  7.         $_POST[$fieldname] != 0)) {
  8.             $field_errors[] = $fieldname;
  9.         }
  10.     }
  11.     return $field_errors;
  12. }
  13.  
  14. function check_max_field_lenghts($field_lenght_array) {
  15.     global $db_cxn;
  16.     $field_errors = array();
  17.     foreach($field_lenght_array as $fieldname => $maxlenght) {
  18.         if (strlen(mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST[$fieldname])))) > $maxlenght) {$field_errors[] = $fieldname;}
  19.         }
  20.         return $field_errors;
  21.     }
  22.  
  23. function display_errors($error_array) {
  24.     echo "<p>Please review the following fields:<br />";
  25.     foreach($error_array as $error) {
  26.         echo " - " . $error . "<br />";
  27.     }
  28.     echo "</p>";
  29. }    
  30. ?>
  31.  


+++THIS IS THE MAIN CODE FOR ADDING A NEW AUTHORIZED USER+++

Expand|Select|Wrap|Line Numbers
  1. <?php require_once("includes/db_connect.php"); ?>
  2. <?php require_once("includes/functions.php"); ?>
  3. <?php
  4.     include_once("includes/form_functions.php");
  5.  
  6.     //START FORM PROCESSING
  7.     if (isset($_POST['submit'])) { //Form has been submitted
  8.         $errors = array();
  9.  
  10.         //PERFORM VALIDATIONS ON THE FORM DATA
  11.         $required_fields = array('username', 'password');
  12.         $errors = array_merge($errors, check_required_fields($required_fields, $_POST));
  13.  
  14.         $fields_with_lenghts = array('username' => 20, 'password' => 40);
  15.         $errors = array_merge($errors, check_max_field_lenghts($fields_with_lenghts, $_POST));
  16.  
  17.         $username = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['username'])));
  18.         $password = mysqli_real_escape_string($db_cxn, trim(htmlspecialchars($_POST['password'])));
  19.         $hashed_password = sha1($password);
  20.  
  21.         if (empty($errors)) {
  22.             $query = "INSERT INTO tblstaff (username, hashed_password) VALUES('{username}', '{hashed_password}')";
  23.  
  24.         $result = mysqli_query($db_cxn, $query);
  25.         if ($result) {
  26.             $message = "The user was successfully created.";
  27.         } else {
  28.             $message = "The user cannot be created.";
  29.             $message.= "<br />" . mysqli_error($db_cxn);
  30.         }
  31.     } else {
  32.         if (count($errors) == 1) {
  33.             $message = "There was 1 error in the form.";
  34.         } else {
  35.             $message = "There were " . count($errors) . " errors in the form.";
  36.             }
  37.         }
  38.     } else { //FORM HAS NOT BEEN SUBMITTED
  39.         $username = "";
  40.         $password = "";
  41.     }
  42. ?>
  43. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  44. <html xmlns="http://www.w3.org/1999/xhtml">
  45. <head>
  46. <title>Restricted Access - For Admin Use Only!</title>
  47. <meta http-equiv="Content-Language" content="English" />
  48. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  49. <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
  50. </head>
  51.  
  52. <body>
  53.  
  54. <div id="wrap">
  55.  
  56. <div id="header">
  57. <div class="logocontainer">
  58.   <div align="center"><img src="images/about/geh.png" alt="gehlogo" width="50" height="48" /></div>
  59. </div>
  60. <h1><a href="index.php">Equipment Information Management System</a></h1>
  61. <h2>Gulf Engineering House Equipment Site</h2>
  62. </div>
  63.  
  64. <div id="menu">
  65.  
  66. </div>
  67.  
  68. <div id="content">
  69. <div class="right">
  70.  
  71. <h2>THIS PAGE IS FOR ADMINS ONLY! UNAUTHORIZED DATA WILL BE DELETED!</h2>
  72. <p>+++ Create a new user +++</p>
  73.     <?php
  74.         if (!empty($message)) {echo "<p>". $message . "</p>";}
  75.         echo "<br />";
  76.         if (!empty($errors)) {display_errors($errors);}
  77.     ?>
  78. <div class="DataInput">
  79.   <table width="336" border="0" cellspacing="0" cellpadding="0">
  80.     <tr>
  81.       <td width="100">Username : </td>
  82.       <td width="236"><input type="text" name="username" id="username" tabindex="1" maxlenght="20"
  83.       value="<?php echo $username; ?>" /></td>
  84.     </tr>
  85.   </table>
  86. </div>
  87. <div class="DataInput">
  88.   <table width="337" border="0" cellspacing="0" cellpadding="0">
  89.     <tr>
  90.       <td width="100">Password :</td>
  91.       <td width="237"><input type="password" name="password" id="password" tabindex="2" maxlenght="40"
  92.       value="<?php echo $password; ?>" /></td>
  93.     </tr>
  94.   </table>
  95. </div>
  96. <div class="DataInput">
  97.  <form action = "restricetdaccess.php" method = "POST">
  98.   <input type="submit" name="submit" id="submit" value="Add New User" tabindex="3" />
  99.  </form>
  100. </div>
  101. <h2><br />
  102.   <br />
  103. </h2>
  104. </div>
  105.  
  106. <div class="left"> 
  107.  
  108. <h2></h2>
  109. <ul>
  110.  
  111. </ul>
  112.  
  113. <h2></h2>
  114. <ul>
  115.  
  116. </ul>
  117.  
  118. </div>
  119.  
  120. <div style="clear: both;"> </div>
  121.  
  122. </div>
  123.  
  124. <div id="bottom"></div>
  125. <div id="footer">
  126.  
  127.  
  128.  
  129. <br />
  130. <br />
  131.  
  132.  
  133. </div>
  134.  
  135. </body>
  136. </html>
  137. <?php
  138.     if (isset($db_cxn)) {
  139.         mysqli_close($db_cxn);
  140.     }
  141. ?>
Oct 11 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.