找出此代码中的错误

时间:2014-03-10 10:04:55

标签: php

<form action="" method="POST">
    UserName <input type="text" name="user"><br>

    PassWord <input type="password" name="pwd"><br>

    <input type="submit" name="login" value="LogIn">
    <?php login(); ?>
</form>

此代码会出现错误

  

致命错误:调用未定义的函数login()   第10行/var/www/trainees/bhupender/cms/admin.php。

这是我的function.php文件编码

function login(){

if(!isset($_POST['username']))
    return;

global $link;
$query = "SELECT * from `b_user` where username = '{$_POST['username']}' AND password = '{$_POST['password']}'";

$result = mysqli_query($link, $query);
$no_of_results = mysqli_num_rows($result);
echo $no_of_results;
if($no_of_results === 1){
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $row['id'];
        $_SESSION['user_id'] = $row['id'];
        header('location: manage.php');
        exit;
} else {
    header('location: index.php');
        exit;
}

}

1 个答案:

答案 0 :(得分:0)

为什么不使用

    <?php
     function login() {
    //function body
}
     if($_POST) {
       login();
    }
    ?>

和你的HTML;

<form action="" method="POST">
    UserName <input type="text" name="user"><br>

    PassWord <input type="password" name="pwd"><br>

    <input type="submit" name="login" value="LogIn">
</form>