简体   繁体   中英

php form variables not defined - what am i doing wrong?

I am trying make my login form work - the database has been created and the user exists - the error message i am getting is this:

Successfully connected to server.
Successfully connected to the MemberDirectory database: 
Notice: Undefined variable: loginEmail in C:\xampp\htdocs\CSC8417\Project\userlogin.php on line 231
Notice: Undefined variable: password in     C:\xampp\htdocs\CSC8417\Project\userlogin.php on line 231
SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...

The code which generates this error is:

<div class="login-form">
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="hidden" name="submit" value="true" class="submit-container"/>
        <table class="form-container">
            <tr><td><label class="form-title">User Email: </td><td><input type="text" name="loginEmail" class="form-field"/></label></td></tr>
            <tr><td><label class="form-title">Password: </td><td><input type="password" id="password" name="password" class="form-field"/></label></td></tr>
            <tr><td></td><td><input type="submit" class="submit-button" name="submit" value="Login" href="account.php"/></td></tr>
        </table>
</div>
<?php
include ('libcommon.php');

if(isset($_POST['submit'])){
$loginEmail =  $_POST['loginEmail']; 
$password   =  $_POST['password'];

}
function SignIn() { 

    if(!empty($_POST['loginEmail'])) {            //checking the 'user' name which is from Sign-In.html, is it empty or have some text 

        $query = mysql_query("SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error()); 
        if(!empty($row['loginEmail']) AND !empty($row['password'])) { 
            $_SESSION['loginEmail'] = $row['password']; 
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 
            } else { 
            echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY..."; 
            } 
    }
}

 SignIn();  

?>

You need to create variable

$loginEmail = $_POST['loginEmail'] 

inside your if statement in function Signin.
Just a suggestion - you should call Signin() inside the if statement checking for form submit.

You are trying to access variable in a scope where it was not accessible. make the variable global using global keyword. Final code:

<div class="login-form">
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="hidden" name="submit" value="true" class="submit-container"/>
        <table class="form-container">
            <tr><td><label class="form-title">User Email: </td><td><input type="text" name="loginEmail" class="form-field"/></label></td></tr>
            <tr><td><label class="form-title">Password: </td><td><input type="password" id="password" name="password" class="form-field"/></label></td></tr>
            <tr><td></td><td><input type="submit" class="submit-button" name="submit" value="Login" href="account.php"/></td></tr>
        </table>
</div>
<?php
include ('libcommon.php');

if(isset($_POST['submit'])){
global $loginEmail =  $_POST['loginEmail']; 
global $password   =  $_POST['password'];

}
function SignIn() { 

    if(!empty($_POST['loginEmail'])) {            //checking the 'user' name which is from Sign-In.html, is it empty or have some text 

        $query = mysql_query("SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error()); 
        if(!empty($row['loginEmail']) AND !empty($row['password'])) { 
            $_SESSION['loginEmail'] = $row['password']; 
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 
            } else { 
            echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY..."; 
            } 
    }
}

 SignIn();  

?>

Now two error will be removed but your code is not perfect.You should code considering all exceptions.

Try this updated code.. set variables in the function "SignIn()"

code:

<div class="login-form">
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input type="hidden" name="submit" value="true" class="submit-container"/>
        <table class="form-container">
            <tr><td><label class="form-title">User Email: </td><td><input type="text" name="loginEmail" class="form-field"/></label></td></tr>
            <tr><td><label class="form-title">Password: </td><td><input type="password" id="password" name="password" class="form-field"/></label></td></tr>
            <tr><td></td><td><input type="submit" class="submit-button" name="submit" value="Login" href="account.php"/></td></tr>
        </table>
</div>
<?php

function SignIn() { 

    if(!empty($_POST['loginEmail']) && isset($_POST['submit']) ) { 
        $loginEmail =  $_POST['loginEmail']; 
        $password   =  $_POST['password'];
        //checking the 'user' name which is from Sign-In.html, is it empty or have some text 
        echo "SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'";
        $query = mysql_query("SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error()); 
        if(!empty($row['loginEmail']) AND !empty($row['password'])) { 
            $_SESSION['loginEmail'] = $row['password']; 
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 
            } else { 
            echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY..."; 
            } 
    }
}

 SignIn(); 

?>

Or try this way, Its will help you!

<?php

function SignIn($loginEmail,$password) { 

    if(!empty($_POST['loginEmail']) && isset($_POST['submit']) ) { 

        //checking the 'user' name which is from Sign-In.html, is it empty or have some text 
        echo "SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'";
        $query = mysql_query("SELECT * FROM members where loginEmail = '$loginEmail' AND password = '$password'") or die(mysql_error()); 
        $row = mysql_fetch_array($query) or die(mysql_error()); 
        if(!empty($row['loginEmail']) AND !empty($row['password'])) { 
            $_SESSION['loginEmail'] = $row['password']; 
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; 
            } else { 
            echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY..."; 
            } 
    }
}

 if(!empty($_POST['loginEmail']) && isset($_POST['submit']) ) { 
     $loginEmail =  $_POST['loginEmail']; 
     $password   =  $_POST['password'];
     SignIn($loginEmail,$password ); 
 }

?>

Thank You!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM