繁体   English   中英

似乎无法从数据库获取电子邮件和密码来登录?

[英]Can't seem to get email and password from DB to Log IN?

总是得到一个错误:( (Call to a member function bind_param() on a non-object error)

我认为我的脚本无法连接到数据库。 数据库只有3个字段,ID,电子邮件,密码。 请任何帮助。

为database.php

<?php
class Database {
    public function __construct() {
        $host = '127.0.0.1';
        $user = 'root';
        $pass = '';
        $name = 'test';
        @$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
    }
}
?>

users.php

<?php

include "database.php";
class Users extends Database {

    public function login($email, $password) {

        if($stmt = $this->mysqli->prepare("SELECT email, password FROM users WHERE `email` = ? AND `password` = ? LIMIT 1")){
            $stmt->bind_param('sssss', $email, $password);
            $stmt->execute();
            $stmt->bind_result($email, $password);
            $stmt->store_result();
            if($stmt->num_rows == 1) {
                while($stmt->fetch()) {
                    $_SESSION['email'] == $email;
                    echo $email;
                }
            } else {
                echo "ERROR";
            }

            $stmt->close();
            $stmt->free_result();

        } else {
            echo"Something went wrong";
        }
    }
} 

$users = new users();

的login.php

<?php

session_start();
include "php/classes/users.php";
if(isset($_POST['login'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $users->login($email, $password);
}

?>

登录表单

<form action="" method="POST" name="login">
    <input type="email" name="email" id="username" placeholder="Vendoseni E-mailin"/><br/>
    <input type="password" name="password" id="pass" placeholder="Vendoseni Passwordin"/><br/>
    <input type="submit" name='login' value="Kycuni" id="login_btn"/>
</form>

您需要在您的课程中提供该属性:

class Database
{
    protected $mysqli; // change visibility
    private $host = '127.0.0.1';
    private $user = 'root';
    private $pass = '';
    private $name = 'test'; // these guys

    public function __construct()
    {
        $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
    }
}

然后在该Users类上:

class Users extends Database
{

    public function login($email, $password)
    {
        if($stmt = $this->mysqli->prepare("SELECT email, password FROM users WHERE `email` = ? AND `password` = ? LIMIT 1")){

            $stmt->bind_param('ss', $email, $password);
                            // ^^ only two placeholders! not ssss
            $stmt->execute();
            $stmt->bind_result($email, $password);
            $stmt->store_result();

            if($stmt->num_rows == 1) {
                while($stmt->fetch()) {
                  $_SESSION['email'] == $email;
                  echo $email;
                }
            } else {
              echo "ERROR";
            }
            $stmt->free_result(); // free the result first, then close
            $stmt->close(); // not the other way around.

        } else {
            echo"Something went wrong";
        }
    }
}

$users = new Users(); // initialize that particular class.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM