-4

I am trying to add address fields into my registration form but whenever I submit the register Im having error with my address line here is what the error message say

Notice: Undefined index: address in C:\xampp\htdocs\profile\register\index.php on line 9

This is the php code:

<?php
    session_start();

    if(isset($_REQUEST['sub']) && isset($_POST['gender'])){
        $lname = strtoupper(trim(strip_tags($_POST['lname'])));
        $fname = strtoupper(trim(strip_tags($_POST['fname'])));
        $mname = strtoupper(trim(strip_tags($_POST['mname'])));
        $cours = strtoupper(trim(strip_tags($_POST['gender'])));
        $address =strtoupper(trim(strip_tags($_POST['address'])));
        $email = strip_tags($_POST['email']);

        $pass1 = strip_tags($_POST['pass']);

        if(empty($lname) || empty($fname) || empty($mname) || empty($email) || empty($address)
            || empty($pass1) || empty($cours) || $_POST['gender'] == 'none' ){
            $mess = "<font color='red'><i>Field(s) Required!</i></font>";
        }
                else if(!ctype_alpha($lname)||!ctype_alpha($fname)|| !ctype_alpha($mname)){
                        $mess = "<font color='red'><i>Incorrect Name Format!</i></font>";
                }
                else if(!preg_match('/^[a-zA-z0-9_.]*@[a-zA-z0-9.]*$/', $email)){
                        $mess = "<font color='red'><i>Incorrect Email Format!</i></font>";
                }
        else{
            $host = "localhost"; 
            $user = "root"; 
            $pass = ""; 
            $data = "school"; 

            $connect = mysql_connect($host, $user, $pass); 
            mysql_select_db($data, $connect); 

            if(mysql_query("INSERT INTO studentinfo(`userid`, `lastname`, `firstname`, `middlename`, `email`, `address`, `password`, `gender`) 
                VALUES (null,'$lname','$fname','$mname','$email',`$address`,'$pass1','$cours') ")){
                $mess = "Account Added to Database!";
            }
            else{
                $mess = "<font color='red'><i>Data Already Exist!</i></font>";
            }
        }
    }

    if(isset($_REQUEST['back'])){
        header( 'Location: ../' );
    }

    if(!empty($_SESSION['logon'])){
        header( 'Location: ../' );
    }   

?>
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53

3 Answers3

1

Before that row check if $_POST['address'] is defined:

if(isset($_POST['address'])){

}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
1

You're probably missing an input field named "address". You can check using PHP:

<?php
// long way around
if(array_key_exists("address",$_POST){
  // do something
}
else{
  // not found
}

// short way
$address = array_key_exists("address",$_POST) ? $_POST["address"] : "";
?>

Ofcourse, you can do this for all fields and add your own code. Otherwise, please show us your form layout. You should have

<input type="text" name="address" />

in your code.

  • Better is to update HTMl form, he probably need to get address from user when he save that into databse :-) – pavel Nov 20 '14 at 08:45
  • 2
    Absolutely true, but I do think it's nice to check if the key exists anyway, just to be sure that your script doesn't generate an error if one field is missing. –  Nov 20 '14 at 08:48
1

Try this way:

if(isset($_REQUEST['sub']) && isset($_POST['gender']))
{
    $address = isset($_POST['address']) ? 
        strtoupper(trim(strip_tags($_POST['address']))) : '';
Ashique C M
  • 733
  • 4
  • 8