2017-10-14 215 views
2

我試圖做一個簡單的註冊系統在PHP中。我有一個名爲Admin的類(內部控制器文件夾),它擴展了DBConnection.php類。管理員類有一個註冊方法讓adming註冊但在那裏遇到問題。錯誤發生在'include_once'處,並且錯誤顯示'Warning:include_once(../ Database/DBConnection.php):未能打開流:C:\ xampp \ htdocs \ WoodlandsAway \ controller \ Admin.php中沒有這樣的文件或目錄第15行----- ' '警告:include_once():在C:\ xampp中打開用於包含(include_path ='C:\ xampp \ php \ PEAR')的'../Database/DBConnection.php'失敗\ htdocs \ WoodlandsAway \ controller \ Admin.php on line 15 ----- ' '致命錯誤:Class'DBConnection'未在C:\ xampp \ htdocs \ WoodlandsAway \ controller \ Admin.php中找到第17行'include_once():未能打開流:沒有這樣的文件或目錄 - PHP

這裏是我的include_once代碼

include_once ('../Database/DBConnection.php'); 

And here is my folder structure

--DBConnection.php

class DBConnection { 
//put your code here 
private $host; 
private $user; 
private $pass; 
private $database; 
private $conn; 

function DBConnection() { 
    $this->host = 'localhost'; 
    $this->user = 'root'; 
    $this->pass = ''; 
    $this->database = 'woodlands_away'; 
} 

public function getConnections() { 
    $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->database) or 
    die($this->conn->error); 

    return $this->conn; 
} 

}

而且admin.php的

include_once ('../Database/DBConnection.php'); 

class Admin extends DBConnection { 
public function Admin() { 
    parent::DBConnection(); 
} 

public function signup($username, $password) { 
    $sql = "insert into users values(".$username.", ".$password.")"; 

    return $this->getConnections()->query($sql); 
}} 
+0

一旦你在你的目錄結構中進行驗證,你就可以使用相對路徑了,一個目錄上並檢查數據庫文件夾是否可用 – Hkachhia

+0

試試'include_once(__DIR__。/../ Database/DBConnection.php')' – HtmHell

+0

是admin.php也包含在內?還是你直接調用該文件? – Jeff

回答

1

首先,我建議你來聲明表示根路徑不變你的項目。該常數必須以一種獨特的方式來聲明這樣的index.php文件或類似,但在你的項目的根目錄:

define('PROJECT_ROOT_PATH', __DIR__); 

那麼你包括呼叫應該看起來像這樣:

include_once (PROJECT_ROOT_PATH . '/Database/DBConnection.php'); 

(始終指定前導斜槓)

問題是,目前您的代碼可能依賴於Working directory,因此您可能會收到意外的工作目錄。

相關問題