为什么我的代码总是返回“用户名和密码错误”?

时间:2014-04-18 15:21:43

标签: php sql forms sqlite

很抱歉,如果这是一个经常出现的问题,但我很难过。

<?php
session_start();
unset($_SESSION['logerror']);

$host="localhost"; // Host name
$db_name="website_database.sqlite"; // Database name
$tbl_name="User"; // Table name

$con= sqlite_open("Website_Database.sqlite");

// username and password sent from form
$username=$_POST['Username'];
$password=$_POST['Password'];


$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."' and Password='".$password."'";
$result= sqlite_query($con,$sql);

// Mysql_num_row is counting table row
$count= sqlite_num_rows($result);

// If result matched $username and $password, table row must be 1 row


if($count==1){

// Register username, $password and redirect to file "login_success.php"
session_register("username");
session_register("password");
echo "Login Successful";
}
else {
echo "does this work nikhil";
}
?>

我正在使用SQLite,它正在运行USB。该文件是website_database.sqlite

该表为User。其中的数据是用户名:000001和密码:密码

来自上一个表单的帖子数据是

<form action="checklogin.php" method="post">
        <div class="wrap"> <!--make a container that will have all our elements and will be the main stucture of you pages-->
            <div class="logo"></div><!--The logo container-->
            <div class="header_text">Room Booking</div><!--The main header of the fomr-->
            <div class="logins_details_container"><!--The top container-->
                <table class="fields_container"> <!--A simple 2x2 table for the form. We choose to use table as for this simple kind of stuff helps us to be everything well stuctured and putting less attributes at the css-->
                    <tr>
                        <td><span>Username</span></td>
                        <td><input class="field" id="Username" name="Username" value="" placeholder="" type="text" /></td>
                    </tr>
                    <tr>
                        <td><span>Password</span></td>
                        <td><input  class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>
                    </tr>
                </table>
                <div class="remeber_me_container"><!--A container for the Remember me text and the checkbox-->
                    <div class="remember_me">Remember Me?</div>
                    <input  id="remeber_me" name="" value="" type="checkbox" />
                </div>
                <div class="login_btn"><!-- The login button-->
                    <input name="login_btn" value="Login" type="submit" />
                </div> 
            </div>
    <form/><!-- End of the logins_details_container --> 

2 个答案:

答案 0 :(得分:1)

在您的表单中,密码输入字段的名称属性为空。我想你会想要这个:

<td><input  class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>

答案 1 :(得分:0)

我只是将此作为临时答案发布,因为评论太多了。

无论出于何种原因,您当前的代码似乎没有发布密码值,所以让我们尝试用一些非常简单的代码缩小它。用此替换checklogin.php中的所有代码并将其激活。输入用户并传递,点击提交,然后让我知道print_r的结果是什么。

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    echo '<form action="checklogin.php" method="post">
    <input name="Username"><br>
    <input type="Password" name="Password"><br>
    <input value="Login" type="submit">
    </form>
    ';
}else{
    print_r($_POST);
}

如果它返回用户名和密码,请将print_r($_POST);替换为您的其他PHP代码,并查看登录是否有效。再次,发布结果......

我唯一能看到的是您的HTML中缺少</div>以及错误的</form>结束标记。你也错过了几次remember_me

<form action="checklogin.php" method="post">
    <div class="wrap"> <!--make a container that will have all our elements and will be the main stucture of you pages-->
        <div class="logo"></div><!--The logo container-->
        <div class="header_text">Room Booking</div><!--The main header of the fomr-->
        <div class="logins_details_container"><!--The top container-->
            <table class="fields_container"> <!--A simple 2x2 table for the form. We choose to use table as for this simple kind of stuff helps us to be everything well stuctured and putting less attributes at the css-->
                <tr>
                    <td><span>Username</span></td>
                    <td><input class="field" id="Username" name="Username" value="" placeholder="" type="text" /></td>
                </tr>
                <tr>
                    <td><span>Password</span></td>
                    <td><input class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>
                </tr>
            </table>
            <div class="remember_me_container"><!--A container for the Remember me text and the checkbox-->
                <div class="remember_me">Remember Me?</div>
                <input id="remember_me" name="" value="" type="checkbox" />
            </div>
            <div class="login_btn"><!-- The login button-->
                <input name="login_btn" value="Login" type="submit" />
            </div> 
        </div>
    </div><!-- THIS ONE!-->
</form><!-- NOT <form/>!--> 
相关问题