为什么我收到错误消息?

时间:2013-03-16 21:57:49

标签: php login

我写了这个“登录”页面,昨晚一切正常,但错误信息没有显示。今天,我去仔细检查我的工作,我收到此错误消息:在第7行的E:\ wwwroot \ teamc \ logIn.php中调用未定义的函数showForm()。我没有更改或更改我的代码从昨晚到今天早上。任何人都可以看到为什么错误信息现在出现了吗?

<?php
session_start();

    //validate text was entered in UserName text box
if(empty($_POST['txtUserName']))
    {
        showForm('Please Enter A User Name.');
        exit();
    }
else
    {
        $UserName = $_POST['txtUserName'];
    }

//validate text was entered in password text box
if(empty($_POST['txtPassword']))
    {
        showForm('Please Enter A Password.');
        exit();
    }
else
    {
        $Password = $_POST['txtPassword'];
    }

if($Password != Password($UserName))
    {
        showForm('User Name And Password Do Not Match!');
        exit();
    }


function Password($UserName)
{
//database login
$dsn = 'mysql:host=XXX;dbname=XXX';
$username='XXX';
$password='XXX';
//variable for errors
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
//try to run code
try {
//object to open database
$db = new PDO($dsn,$username,$password, $options);
//check username against password
    $SQL = $db->prepare("Select USER_PASSWORD FROM user WHERE user_name =  :UserName");
    $SQL->bindValue(':UserName', $UserName);
    $SQL->execute();
    $username = $SQL->fetch();

    if($username === false)
        {
            $password = null;
        }
    else
        {
            $password = $username['USER_PASSWORD'];
            include 'index.php';
        }

    return $password;
    $SQL->closeCursor();
    $db = null;

    } catch(PDOException $e){
        $error_message = $e->getMessage();
        echo("<p>Database Error: $error_message</p>");
        exit();
    }

}
function showForm($formMessage)
{?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WisCon Log In</title>
<link rel="stylesheet" href="styles/wiscon-default-styles.css" type="text/css" />
<link rel="stylesheet" href="styles/FormStyle.css" type="text/css" />
<script type="text/javascript" src="js/validateLogInForm.js/validateLogInForm.js">  </script>
</head>

<body id="logPage">
<div id="wrapper">

    <?php include('includes/header.php'); ?>
    <?php include('includes/topNavigation.php'); ?>

    <div id="mainContent">

        <div class="formDiv">
            <form name="registerForm" id="registerForm" action="" method="post">
                <h1 style="color:#FF530D; text-align: center">Log into your account here!</h1>

                <fieldset id="security">
                    <legend>Security</legend>
                    <label for="txtUserName" class="boxLabel">User Name:</label>
                    <input type="text" id="txtUserName" name="txtUserName" autofocus="autofocus" required="required" />
                    <script type="text/javascript">
                        if(!("autofocus" in document.createElement("input")))
                        {
                            setTimeout(function(){
                                document.getElementById("txtUserName").focus();
                            }, 10);

                        }
                        </script>
                    <label for="txtPassword" class="boxLabel">Password:</label>
                    <input type="password" id="txtPassword" name="txtPassword" required="required" />
                </fieldset>

                <fieldset id="submission">
                    <div id="buttons">
                        <input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" onclick="return validateLogInForm()"/>
                        <input type="reset" id="btnReset" name="btnReset" >
                    </div><!--end buttons-->
                </fieldset>
</p>
            </form>
        </div><!--end div class=formDiv-->
    </div><!--end div id=mainContent-->

    <?php include('includes/footer.php'); ?>

</div><!--end div id=wrapper-->
</body>
</html>
<?php
}
?>

2 个答案:

答案 0 :(得分:0)

为什么在调用函数后使用exit()? 尝试删除它,看它是否有效。

此外,您没有在html代码中看到错误消息的原因仅仅是因为您没有在html输出中的某处调用传递的变量$formMessage

答案 1 :(得分:0)

来自手册exit()

  

输出消息并终止当前脚本

所以之后的任何事情,包括你定义函数的地方都不会被执行

没有必要在exit()中使用if/else因为else返回if时不会执行true

修改
如果exit()中的if/else是必需的,请将您的function showForm($formMessage)移到if之上,以防止其被执行/无法调用。

编辑2
要使错误消息可见,请尝试添加类似的内容 -

<?php if($formMessage !="") echo "<h2 style=\"color:#FF0000; text-align: center\">".$formMessage."</h2>"; ?>

之间

...
<h1 style="color:#FF530D; text-align: center">Log into your account here!</h1>
<?php if($formMessage !="") echo "<h2 style=\"color:##FF0000; text-align: center\">".$formMessage."</h2>"; ?>
<fieldset id="security">
...