2017-07-05 72 views
-2
// connection.php file 

<?php 


class Connection{ 

    private $host; 
    private $username; 
    private $password; 
    private $database; 

    public function __construct($host,$username,$password,$database) 
    { 
    try{ 
    $conn = new PDO("mysql:host=".$host.";dbname=".$database, $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    return $conn; 
    }catch(PDOException $e) 
    { 
     return $e->getMessage(); 
    } 
    } 

} 

?> 

// user.php 

<?php 

class Users extends Connection{ 

    private $username; 
    private $email; 
    private $password; 

    public function __construct($username,$email,$password){ 

    $this->username=$username; 
    $this->email=$email; 
    $this->password=$password; 

    } 

    public function addUser(){ 
    $conn = new Connection("localhost","root","","userdata"); 
    echo "database connection established"; 
    die(); 
    $sql = $conn->prepare("INSERT INTO information (UserName, Email, Password) VALUES (?, ?, ?)"); 
    $conn->bind_param("sss", $username, $email, $password); 
    $username=this->username; 
    $password=this->password; 
    $email=this->email; 
    $sql->execute(); 

    echo "New records created successfully"; 

    $sql->close(); 
    $conn->close(); 

    } 
} 

?> 

process.php 

<?php 

include('lib/connection.php'); 

if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password'])) 
{ 
    $user = new User("Gautam","[email protected]","123456"); 
    $user->addUser(); 
} 

?> 

因此,當我嘗試執行process.php文件中的記錄不被添加到該database.So我已經建立了問題的單獨連接類,其中i已寫入的邏輯數據庫連接,然後我將該類繼承給用戶,我將用戶添加到數據庫,然後我在process.php中調用了用戶類的對象,但記錄未添加到數據庫中。請澄清與此相關的問題。面向對象的PHP代碼錯誤

+1

你能補充這個錯誤信息嗎? – Yupik

+0

顯示'bind_param'方法的實現。 –

+3

「請澄清是什麼問題」的確如此。 –

回答

-1

首先使用$ this代替這個在你的類中,然後你調用錯誤的類名User,它是Users,請修改並嘗試,應該工作。

$user = new Users("Gautam","[email protected]","123456"); 

,並在類用戶

$username=$this->username; 
$password=$this->password; 
$email=$this->email; 

變化上面,它應該工作的你的adduser功能..

+0

好友我已經不用它仍然沒有工作 – GeekyGautam

+0

還有一件事,把這條線像回聲「建立數據庫連接」的條件;死(); –

0

與您的代碼的問題是,你沒有執行之前綁定任何參數。

檢查這些行:

$sql = $conn->prepare() 
$conn->bind_param("sss", $username, $email, $password); 

這條線應該是$sql->bind_param("sss", $username, $email, $password);

和還指出,PDO沒有bind_param()功能它都有:bindParam()

因此你需要

$sql->bindParam(1, $this->username); 
    $sql->bindParam(2, $this->email); 
    $sql->bindParam(3, $this->password); 

下面這段代碼應工作:

<?php 

class Users extends Connection 
{ 

    private $username; 
    private $email; 
    private $password; 

    public function __construct($username, $email, $password) 
    { 
     $this->username = $username; 
     $this->email = $email; 
     $this->password = $password; 

    } 

    public function addUser() 
    { 
     $conn = new Connection("localhost", "root", "", "userdata"); 
     echo "database connection established"; 

     $username = $this->username; 
     $password = $this->password; 
     $email = $this->email; 



     $sql = $conn->prepare("INSERT INTO information (UserName, Email, Password) VALUES (?, ?, ?)")->execute(array($username,$email,$password)); 

     if (!$sql) { 

      print_r("error : " . $conn->errorInfo()); 
     } else { 

      echo "New records created successfully"; 
     } 
     $sql->close(); 
     $conn->close(); 
    } 
} 

?> 

PS:不要以純文本形式存儲密碼,使用PHP的內置功能,例如 爲password_hash()password_verify()準備你的密碼。