php表单变量未定义 - 我做错了什么?

时间:2015-06-25 07:50:52

标签: php html login

我正在尝试让我的登录表单工作 - 数据库已经创建并且用户存在 - 我得到的错误信息是:

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...

生成此错误的代码是:

<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();  

?>

3 个答案:

答案 0 :(得分:0)

您需要创建变量

$loginEmail = $_POST['loginEmail'] 

在函数Signin中的if语句中 只是一个建议 - 您应该在if语句中调用Signin()检查表单提交。

答案 1 :(得分:0)

您正尝试访问无法访问的范围中的变量。使用global关键字使变量成为全局变量。最终代码:

<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();  

?>

现在将删除两个错误,但您的代码并不完美。您应该考虑所有异常的代码。

答案 2 :(得分:0)

尝试更新的代码..在函数&#34; SignIn()&#34;

中设置变量

代码:

<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(); 

?>

或者尝试这种方式,它会帮助你!

<?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 ); 
 }

?>

谢谢!!