undefined index problem

Discussion in 'PHP' started by newbie12345, May 4, 2010.

  1. #1
    hey im getting ah little problem when trying to send information to the database

    these are the error im getting

    Notice: Undefined index: u_name in C:\wamp\www\music website\register.php on line 104

    Notice: Undefined index: f_name in C:\wamp\www\music website\register.php on line 105

    Notice: Undefined index: l_name in C:\wamp\www\music website\register.php on line 106

    Notice: Undefined index: DOB in C:\wamp\www\music website\register.php on line 107

    Notice: Undefined index: gender in C:\wamp\www\music website\register.php on line 108

    Notice: Undefined index: address in C:\wamp\www\music website\register.php on line 109

    Notice: Undefined index: pwd in C:\wamp\www\music website\register.php on line 110

    Notice: Undefined index: pwd1 in C:\wamp\www\music website\register.php on line 111

    Notice: Undefined index: e_address in C:\wamp\www\music website\register.php on line 112

    and when i refresh the page the database is updating with 1 record but nothing in the fields

    
    <?php
    include ('connect.php');
    ?>
    
    
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Street Vybz Muzik | Register</title>
    <link href="registerstyle.css" rel="stylesheet" type="text/css" />
    
    <script src="register.js">
    </script>
    
    
    
    
    
    </head>
    
    <body>
    <div id="whole_page_content">
    <div id="logo_image"> </div>
    
    <div id="link_bg">
    <div id="menu">
    <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="news.html">News</a></li>
    <li><a href="music.html">Music</a></li>
    <li><a href="register.html">Register</a></li>
    </ul>
    </div>
    
    
    </div>
    <div id="other_link_background"></div>
    
    <div id="member_registration">MEMBER REGISTRATION---PLEASE FILL OUT THE FOLLOWING FIELDS</div>
    
    <div id="login_content">
    
    
    
    <form name="registration"  action="register.php" method="POST" onSubmit="return validation(this);">
    USERNAME:<br> 
      Between 5-15 characters. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="u_name" type="text"  size="35" maxlength="50">
      <br><br>
      FIRSTNAME:
      <br>
       Enter your first name. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="f_name" type="text" size="35" maxlength="50">
      <br><br>
      LASTNAME:
      <br>
      Enter your last name.
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="l_name" type="text" size="35" maxlength="50">
      <br><br>
    DATE OF BIRTH:
      <br>
      Enter your Date of Birth.
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="DOB" type="text" size="35" maxlength="50"> (in MM/DD/YYYY format)
      <br><br>
      Gender
      <br>
      Select your gender.
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="gender" type="radio" value="male">MALE
      <input name="gender" type="radio" value="female">FEMALE
      <br><br>
    ADDRESS:
      <br>
      Enter your ADDRESS
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="address" type="text" size="50" maxlength="50">
      <br><br>
      PASSWORD:
      <br>
      More than 5 characters.          
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <input name="pwd" type="password" size="35" maxlength="50">
      <br><br>
      VERIFY PASSWORD:
      <br>
      Verify your password.
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
     <input name="pwd1" type="password" size="35" maxlength="50">
      <br><br>
      EMAIL:
      <br>
      Enter your email address.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="e_address" type="text" size="35" maxlength="50">
      <br><br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Register" name="submit">
      <input name="reset" type="reset" value="Reset Information">
    </form>
    
    <?php
          $user  = $_POST['u_name'];
    	  $first  = $_POST['f_name'];
    	  $last  = $_POST['l_name'];
    	  $birth   = $_POST['DOB'];
    	  $sex        = $_POST['gender'];
    	  $addres    = $_POST['address'];
    	  $pass   = $_POST['pwd'];
    	  $conpass    = $_POST['pwd1'];
    	  $eaddress  = $_POST['e_address'];
    	  
    
    
    
    
    
    $result= mysql_query("INSERT INTO user_accounts (username, firstname, lastname, birthday, gender, address, password, confirm_password, email_address)
    VALUES ('$user', '$first', '$last', '$birth', '$sex', '$addres', '$pass', '$conpass', '$eaddress')");
    
    
    
    
    
    
    
    
    
    
    
    ?>
    																																																								
    																																																								
    																																																								
    																																																																																																																	
    
    </div>
    
    
    
    
    
    
    
    
    
    
    
    
    
    </div>
    </body>
    </html>
    
    PHP:
     
    newbie12345, May 4, 2010 IP
  2. demiRon

    demiRon Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you dont want to get these errors you can add : error_reporting(0);
    or
    you can add "@" each $_POST .

    ex: $user = @$_POST['u_name'];
     
    demiRon, May 4, 2010 IP
  3. easic

    easic Active Member

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #3
    I think the best option here is to check if the index is set, not hide errors.

    Example:
    
     $user = (isset($_POST['u_name'])) ? $_POST['u_name'] : ''; // this is the ternary notation
    
    Code (markup):
    If you don't understand the code above, you can get the same result:

    
    if (isset($_POST['u_name'])) $user = $_POST['u_name'];
    else $user = '';
    
    Code (markup):
    That is, if it is not set, $user is empty, otherwise, $user has the same value as $_POST['u_name'].

    My other advice is to use arrays to store post info, like:

    $data['user'] in replacement of $user, $data['first'] .... etc. This is just to not get headaches with register_globals (which, soon - i hope, will be vanished forever).
     
    easic, May 4, 2010 IP