473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Undefined Index Error While Inputting A New User

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
0 1604

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

Similar topics

1
by: shalini11 | last post by:
Hello, I am using a page that has a dropdown list. when the user selects an item from the list and click on submit button, it opens a new page and displays the selected item. I've used as method...
6
by: jsgoodrich | last post by:
I am looking for some help if anyone can lend a hand. I have a simple php website that displays a table from my mysql database. To prep for my MCSE I moved my home server to Windows 2003...
11
by: Lucylew | last post by:
ive got a box i want to click which will add the product to the basket but i keep gettin the undefined index error <?include("conninfo.php")?> <?session_start();?> <html> <head>...
3
by: JJM0926 | last post by:
I've got a contact form with a submit button where users have to enter their support information. Some fields are required some are not. When I test out the form if I leave everything blank I get...
7
by: ridgedale | last post by:
I wonder if anyone can help resolve an issue I've come across in making a customer's website live when it has tested fine in my own hosting space. I am getting the following errors: Notice:...
7
by: Philth | last post by:
Hey there, it has to be said that I am a complete novice in PHP, I know this is a common error and has probably been covered to some degree already - but my head really does hurt - so I couldn't...
1
by: jason1987 | last post by:
I keep getting the undefined index error on the bottom two rows of my code simparent and equtype - the other two work fine yet there is no difference in my code. equtypeid is even from the same table...
6
by: nadomt | last post by:
Happy friday to all! I am getting the below error when visiting my user filter page. i have gone to line 68, but cant see what the problem is...can anyone assist? advanced THANK YOU! Error:...
3
by: furqi | last post by:
hi i am actually having problem in this code i am trying to check the value in the if condition in the very last lines of the code but i am facing an error of "undefined index" can anybody tell me so...
3
by: mediator | last post by:
Hello I have a php contact email form on my a ecommerce shop website ! I am getting a Undefined index error messages coming from the top of the php code, the email form its self works fine, ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.