2012-11-15 66 views
0

我試圖創建一個PHP和mysqli的一個簡單的登錄,但一直收到錯誤來驗證登錄: 致命錯誤:調用一個成員函數準備()一個非對象在C:\瓦帕\ WWW \ WEB2 \ database.php中上線107如何使用的mysqli

database.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');  
     } 

    } 


} 

?> 

的login.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> 

admin.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"); 
    } 
} 

?> 

遺憾的是麻煩,謝謝。

+0

107哪裏?這是很多代碼需要通過的,而且我們還有更多的事情要做,比計算行數還要多。 –

+0

我們對此深感抱歉刪除了一些不相關的代碼,放在107的正確路線 – normower

回答

2

錯字:

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

PHP變量名稱區分大小寫。在你的連接方法中,你使用dbLocalhost和一個小的h

+0

非常感謝。 – normower

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:"); 

(注意 '主機' 的情況下)

+0

由於使用的馬克·B的建議,因爲這意味着改變更少的代碼 – normower

+0

這很好,六一分分的方式,半打其他。但是爲了保持一致性,你應該改變數據庫類頂部的聲明,以防萬一你沒有發現它。 – femtoRgon