0

i have a form in html. when user hit sumbit. i want to run top php code.
when i run it it give me 2 errors

Notice: Undefined variable: _SESSION in C:\xampp\htdocs\login_test\register.php on line 134
Warning: array_key_exists() expects parameter 2 to be array, null given in C:\xampp\htdocs\login_test\register.php on line 134

    <?php
    include("connect.php");

    //grab submitted data
    echo "flag 1";
    if(isset($_POST['submit']))             //-------DOES NOT GO INSIDE THIS IF STATMENT-----------------
    {
        echo "workinging";  
        $reg_error = "";
        echo "herere";
        //check for empty fields
        if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['username']) || 
        empty($_POST['password']) || empty($_POST['confirm_password']) || empty($_POST['dob_year']) 
        || empty($_POST['dob_month']) || empty($_POST['dob_day']) || empty($_POST['gender']))
        {
            $reg_error .= "Missing field! Please fill in  all field!";
        }
        else 
        {
            //grab submitted data   
            $firstname =  $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $confirm_password = $_POST['confirm_password'];

            $dob_year = $_POST['dob_year'];
            $dob_month = $_POST['dob_month'];
            $dob_day = $_POST['dob_day'];
            $gender = $_POST['gender']; 

            //check len of firstname, lastname, username
            if(strlen($firstname) > 20 || strlen($lastname) > 20 && strlen($username) > 20)
            {   
                $reg_error .= "Fistname, lastnmae and username must be no more than 25 characters!";
            }
            else
            {
                //check len of passwords
                if(strlen($password) > 25 || strlen($password) < 6)
                {
                    $reg_error .= "Password must be between 6 and 25 characters!";
                }   
                else
                {
                    //compare password
                    if($password != $confirm_password)
                    {
                        $reg_error .= "Password do not match!";
                    }
                    else
                    {
                        //check for dob is number
                        if(!(is_numeric($dob_year) || is_numeric($dob_month) || is_numeric($dob_day)))
                        {
                            $reg_error .= "Date of birth must be in number form. For example, 1989/06/09";
                        }
                        else
                        {
                            //check value limit of dob
                            if(strlen($dob_year) > 4 || strlen($dob_month)>2 || strlen($dob_day)>2)
                            {
                                $reg_error .= "Date of birth year musb be 4 characters, month and day must be 2 characters.";
                            }
                            else
                            {
                                //check value of dob
                                if($dob_month > 12 || $dob_day > 31)
                                {
                                    $reg_error .= "Date of birth month or day is bigger than expected!";
                                }
                                else
                                {
                                    //check for gender
                                    if($gender != "Male" && $gender != "Female")
                                    {
                                        $reg_error .= "Gender must be Male or Female!";
                                    }
                                    else
                                    {
                                        //check for existing user
                                        $query = mysql_query("Select * FROM user WHERE username = '$username'");        
                                        if(mysql_num_rows($query) >= 1)
                                        {
                                            $reg_error .= "That username is already taken";
                                        }
                                        else
                                        {
                                            //success
                                            $dob_db = "$dob_year-$dob_month-$dob_day";
                                            $password_db = $password;
                                            switch($gender)
                                            {
                                                case "Male";
                                                    $gender_db = "M";
                                                break;
                                                    case"Female";
                                                $gender_db = "F";
                                                break;
                                            }
                                            $register = mysql_query("INSERT INTO register VALUES('','$firstname','$lastname','$username','$password','$dob','$gender')");
                                            //header('Location: index.php');
                                            echo"WORKS";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
            $_SESSION['reg_error'] = $reg_error;      //for errors
            header('Location: register.php');         //go back to register page    
    }   
    else
    {
    echo "dead";
    }
    ?>




  <html>
    <title>Register</title>
    <head>
    <link rel = "stylesheet" href = "css.css">
    </head>
    <body>
                             <!-- go to top php -->
        <form action="<?php echo $_SERVER['$PHP_SELF']; ?>" method="post">
            <?php
            //print errors
            if(array_key_exists('reg_error', $_SESSION) && !empty($_SESSION['reg_error']))
            {
                echo $_SESSION['reg_error'];
                unset($_SESSION['reg_error']);
            }
            ?>
        Firstname: <input class = "text" type="text" name="firstname"><br>
        Lastname: <input class = "text" type="text" name="lastname"><br>
        Username: <input class = "text" type="text" name="username"><br>
        <!-- HINT -->
        <p class="hint">20 characters maximum (letters and numbers only)</p>
        Password: <input type="password" name="password"><br>
        <!-- HINT -->
        <p class="hint">20 characters maximum (letters and numbers only)</p>                  
        Confirm your password: <input type="password" name="confirm_password" size="30"><br>
        <!-- HINT -->
        <p class="hint">20 characters maximum (letters and numbers only)</p>
        Email: <input type="text" name="email"><br/>
        DOB:
            <input type='text' name='dob_year' maxlength='4' size='3' value='YYYY'> / 
            <input type='text' name='dob_month' maxlength='2' size='1' value='MM'> /
            <input type='text' name='dob_day' maxlength='2' size='1' value='DD'> <br/>
        Gender:
            <select name="gender">
                 <option>Female</option>
                <option>Male</option>
             </select> 
        <input type="submit" name="submit" value="Register">
        </form>
    </body>
    </html>
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by fobos
0

If im not mistaken, array_key_exists checks to see if an array is true. From the php manual:
array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index.
Example:

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>

So using your statement will not work, and the error for $_SESSION is becuase you dont have a session variable like $_SESSION['reg_error']

if(array_key_exists('reg_error', $_SESSION) && !empty($_SESSION['reg_error']))

Hope this clarifies things.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.