0

我正在尝试使用 php 和 mysqli 创建一个简单的登录,但一直收到错误消息:致命错误:在线 C:\wamp\www\web2\database.php 中的非对象上调用成员函数 prepare() 107

数据库.php

 <?php
class Database 
{ 
 public $server = "localhost";
 public $database = "database"; 
 public $user = "root"; 
 public $password = ""; 
 public $row;
 public $result;
 public $sqlExtra = " ORDER BY firstname ASC";
 public $dbLocalHost;





 //call connection method upon constructing 
 public function __construct(){
  $this->createConnection(); 
 }

 //connection to the database
 public function createConnection() 
    { 
     $this->dbLocalhost = new mysqli($this->server, $this->user, $this->password, $this->database)
                or die("could not connect:");


      //mysql_select_db($this->database)
       //
       //  or die("Could not find the database: ".mysql_error());



 } 



    function verifyLogin($email, $password) {

        $query = "SELECT *
                FROM admint
                WHERE email = ?
                AND password = ?
                LIMIT 1";

 107.               $statement = $this->dbLocalHost->prepare($query);
        if($statement) {
            $statement->bind_param('ss', $email, $password);
            $statement->execute();

            if($statement->fetch()) {
                $statement->close();
                return true;    
            }
            else {
                return false;
            }
        }

    }

    function addElement($table, $firstName, $lastName, $email, $mobile, $password,
                            $faculty, $campus) {



        $query = "INSERT INTO $table (first_name, last_name, email, mobile, password, faculty, campus_location) 
                VALUES('$firstName', '$lastName','$email','$mobile',
                '$password','$faculty','$campus');";

        if($this->connection->query($query)) {
            header('location: regConfirm.php');     
        }

    }


} 

?>

登录.php

 <?php
session_start();

require_once 'admin.php';
$admin = new Admin();

if(isset($_GET['status']) && $_GET['status'] == 'loggedout') {
    $admin->logOut();   
}


if($_POST && !empty($_POST['email']) && !empty($_POST['password'])) {

    $valid = $admin->validateUser($_POST['email'], $_POST['password']);

    if($valid) {
        header("location: index.php");  
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>

<body>
<div id="login">
    <form method="post" action="">
    <h1>Admin Login</h1>
    <p>
        Email: 
        <input type="email" name="email" />
    </p>

    <p>
        Password: 
        <input type="password" name="password" />
    </p>

    <p>
        <input type="submit" value="Login" name="Submit" />
    </p>

    </form>

</div>

</body>
</html>

管理员.php

    <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author Matt
 */
require 'database.php';
class admin {

   function logOut() {
        if(isset($_SESSION['status'])) {
            unset($_SESSION['status']);

            if(isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time() - 1000);
                session_destroy();  
            }
        }
    }

    function validateUser($email, $password) {
        $db = new Database();

        $verified = $db->verifyLogin($email, $password);

        if($verified) {
            $_SESSION['status'] = 'authorised';
            //header("location: index.php");
            return true;    
        }
        else {
            return false;   
        }
    }

    function confirmAdmin() {
        session_start();
        if($_SESSION['status'] != 'authorised')
            header("location: login.php");  
    }
}

?>

很抱歉打扰了,谢谢。

4

2 回答 2

2

在数据库->createConnection

$this->dbLocalhost = new mysqli($this->server, $this->user, $this->password, $this->database)
            or die("could not connect:");

需要是

$this->dbLocalHost = new mysqli($this->server, $this->user, $this->password, $this->database)
            or die("could not connect:");

(注意“主机”的情况)

于 2012-11-15T18:08:33.590 回答
2

错字:

           $statement = $this->dbLocalHost->prepare($query);
                                      ^--- should be an "h"

php 变量名区分大小写。在您的连接方法中,您使用dbLocalhost一个小的h.

于 2012-11-15T18:08:35.103 回答