I am new to PHP OOP, so I have problem here. I've searched a lot, but couldn't find any useful information. I am trying to add the users to the database and my code doesn't adding any users to it and it doesn't print any error result. Could someone help me to solve this problem? Where am I doing a mistake? Thank you for any help!
Here is my database.php file:
class Database
{
private $host = 'DB_HOST';
private $user = 'DB_USER';
private $password = 'DB_PASSWORD';
private $dbName = 'DB_NAME';
private $dbh;
private $error;
private $stmt;
private $name;
private $lastname;
private $employmentDate;
public function __construct()
{
// Setting Database Source Name (DSN)
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbName;
// Setting options
$options = array(
PDO::ATTR_PERSISTENT => TRUE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Making the connection to the database
try {
$this->dbh = new PDO($dsn, $this->user, $this->password, $options);
} catch(PDOException $e) {
$this->error = $e->getMessage();
}
}
// Avoiding SQL injection with prepare statement
public function query($query)
{
$this->stmt = $this->dbh->prepare($query);
}
// Binding the values
public function bind($param, $value, $type = NULL)
{
if(is_null($type)) {
switch(TRUE) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Executing the prepared statement
public function execute()
{
return $this->stmt->execute();
}
public function insertUserValues()
{
$database->query('INSERT INTO employee (name,surname,employment_date)
VALUES (:name, :surname, :employmentDate)');
}
public function bindingTheValues()
{
$database->bind(':name', $this->name = $_POST['name']);
$database->bind(':lastname', $this->lastname = $_POST['lastname']);
$database->bind(':employmentDate', $this->employmentDate = $_POST['employmentDate']);
$database->execute();
}
}
// Instantiate database
$database = new Database();
?>
I know that my code is quite long, but I am just trying to insert all the stuff that I've been written.
And here is my entryform.php file:
<?php
// Include database class
include 'database.php';
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "employees");
?>
Thank you for any help...