PHP 7和mysqli登录无法正常工作

时间:2018-03-05 07:13:22

标签: php mysql

我尝试创建登录但代码始终返回false?有人可以指导我什么不对。

登录页面

<?php
    $conn=mysqli_connect("localhost","root","","aj") or("error" .mysqli_error($conn));
    mysqli_select_db($conn,"aj");
?> 
<html>
<body>
    <form method="post" action="login.php">
        <div class="c">
            <h1>LOGIN PAGE</h1>
        </div>
        <div class="b">
            <div class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="card">
                                <div class="login-form-main-message"></div>
                                <div class="main-login-form">
                                    <div class="a">
                                        <div class="row">
                                            <div class="col-md-4">
                                                <div class="card">
                                                    <label>Name:</label>
                                                    <br>
                                                    <input type="text" name="uname" class="form-control" value="">
                                                    <br>
                                                    <label>Password:</label>
                                                    <br>
                                                    <input type="password" name="pass" class="form-control" placeholder="Enter Password">
                                                    <br>
                                                    <button type="submit" class="btn btn-success" name="submit" value="login">Login</button>
                                                    <button type="reset" class="btn btn-danger">Cancels</button>
                                                    <br>
                                                    <br>
                                                    <br>
                                                    <a href="reg.php">Not Registered? Click here</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body> 
</html>

此处是操作页面 Login.php -

<?php
    $conn=mysqli_connect("localhost","root","") or die("Some error occurred during connection " . mysqli_error($conn));
    mysqli_select_db($conn,"aj");
    session_start();
    $out='';
    if (isset($_POST['submit'])) {
        $name=isset($_POST['name']) ? $_POST['name'] : '';
        $pass=isset($_POST['pass']) ? $_POST['pass'] : '';

        if (empty($name)&&empty($pass)) {
            echo"Please enter details";
        } else {
            $select="select id from login where name='$name' && pass='$pass'";
            $result=mysqli_query($conn,$select);
            $row = mysqli_num_rows($result);
            if ($row == 1) {
                $_SESSION['user']=$name;// Initializing Session with value of PHP Variable
                header("location:welcome.php");
            } else {
                echo"fail";
            }
        }
    }
?>

它总是返回false并显示失败?怎么了?

请指导我。我刚接触到php 7和mysqli .no错误只显示登录返回false

1 个答案:

答案 0 :(得分:3)

你有一个错字。您的登录字段称为“uname”,而“name”是supposer,我想。那么,为什么在第一个文件中需要上面的PHP块?这没用。

另外,作为良好代码风格的建议,我建议您:

1)带有反引号(`)

的环绕列和表名

2)HTTP标头以大写形式发送。也许PHP header()函数会为你做,但为什么不正确自己:)

3)在{signs和around =运算符

之前,你还有一些与空格不一致的地方

4)至于我,我使用大写进行SQL查询,但这个选择取决于你。