This question already has an answer here:
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):
Here is a connection file:
<?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();
?>
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();
?>
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.
</div>