// 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代碼錯誤
你能補充這個錯誤信息嗎? – Yupik
顯示'bind_param'方法的實現。 –
「請澄清是什麼問題」的確如此。 –