我知道有很多与此错误相关的答案,但我仍然不知道如何解决它...我正在尝试建立数据库连接,它将连接到数据库并插入用户输入其中的值,我收到了这个错误.我创建了 2 个文件(具有不同的类):
I know there were a lot of answers related to this error, but I still don't know how to solve it... I'm trying to make the database connection, which would connect to the database and insert user's entered values in it and i got this error. I've created 2 files (with different classes):
这是一个连接文件:
<?php class Connection { // Setting Database Source Name (DSN) public function __construct() { $dsn = 'mysql:host=localhost;dbname=employees'; // Setting options $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); // Making the connection to the database try { $this->dbh = new PDO($dsn, 'root', '', $options); } catch (PDOException $e) { $this->error = $e->getMessage(); } } } $connection = new connection(); ?>这里是 users.php 文件:
And here is users.php file:
<?php include 'connection.php'; class Users { public $name; public $surname; public $employmentDate; public function __construct() { if(isset($_POST['Submit'])) { $this->name = $_POST['name']; $this->surname = $_POST['surname']; $this->employmentDate = $_POST['employmentDate']; } } // Inserting users values to the database table public function insertUserValues() { $stmt= 'INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)'; $stmt = $this->dbh->prepare(); $stmt->bindValue(':name',$name, PDO::PARAM_STR); $stmt->bindValue(':surname',$surname, PDO::PARAM_STR); $stmt->bindValue(':employmenDate',$employmentDate, PDO::PARAM_STR); $stmt->execute([$this->name,$this->surname,$this->employmentDate]); } } $users = new Users(); $users->insertUserValues(); ?>我猜代码结构有一些错误,但我只是在学习,所以.在 users.php 文件中抛出错误 18 行的代码行:
I guess there are some mistakes in code structure, but I'm just learning, so. The code line which throws the error 18 line in users.php file:
$stmt = $this->dbh->prepare();请有人告诉我我哪里做错了,谢谢您的帮助.
Please someone tell me where I am doing a mistake, thank you for any help.
推荐答案您只是在代码中存在一些错误.尝试使用这一行:
You just have somes mistakes in your code. Try to use this lines :
连接文件:
<?php class Connection { public $dbh; // Setting Database Source Name (DSN) public function __construct() { $dsn = 'mysql:host=localhost;dbname=employees'; // Setting options $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); // Making the connection to the database try { $this->dbh = new PDO($dsn, 'root', '', $options); } catch (PDOException $e) { $this->error = $e->getMessage(); } } } $connection = new connection();users.php 文件:
users.php file :
<?php include 'connection.php'; class Users { public $name; public $surname; public $employmentDate; public $connection; public function __construct($connection) { $this->connection = $connection; if(isset($_POST['Submit'])) { $this->name = $_POST['name']; $this->surname = $_POST['surname']; $this->employmentDate = $_POST['employmentDate']; } } // Inserting users values to the database table public function insertUserValues() { $query = 'INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)'; $stmt = $this->connection->dbh->prepare($query); $stmt->bindValue(':name',$this->name, PDO::PARAM_STR); $stmt->bindValue(':surname',$this->surname, PDO::PARAM_STR); $stmt->bindValue(':employmentDate',$this->employmentDate, PDO::PARAM_STR); $stmt->execute(); } } $users = new Users($connection); $users->insertUserValues();说明:
- 您必须将 $connection 变量传递给您的用户类(或使用 global $connection; 导入它)
- 您的连接文件必须使 dbh 属性可见,否则您将无法对数据库进行任何查询
- PDO prepare() 方法正在等待第一个参数中的查询
- 如果之前已经绑定了值,则不需要将数组传递给 execute() 方法
- You have to pass the $connection variable to your users class (or import it with global $connection;)
- Your connection file has to make visible the dbh property, otherwise you will not be able to make any query on your database
- PDO prepare() method is waiting for a query in first argument
- You don't need to pass an array to execute() method if you already have binded your values before
更多推荐
未捕获的错误:在出现空错误时调用成员函数 prepare()
发布评论