1

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.

user7435747
  • 417
  • 2
  • 5
  • 11

3 Answers3

1

You just have somes mistakes in your code. Try to use this lines :

Connection file :

<?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 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();

Explanations :

  • 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
Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
0

You are referring to $this from outside the class scope. You probably want

$connection->dbh->prepare();
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • I've just changed my line with yours, but it didn't helped and I got one new eror, which says that connection is undefined – user7435747 Mar 07 '17 at 08:48
  • It is, because you use it in the function and it is a global. Add `global $connection` to the top of your function. You issue here is not about PDO, but about scope and variables. Make sure you understand PHP basics before you continue with database stuff. – Bart Friederichs Mar 07 '17 at 08:55
0

You could make your connection class to a singleton:

class Connection {

    private static $instance = null;

    // 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();
        }
    }

    public static function __callStatic($name, $args) {
        if (self::$instance == null) {
             self::$instance = new self();
        }
        call_user_func_array([ self::$instance->dbh, $name], $args);
    }   

}

Then use it as follows:

 Connection::prepare(); //or connection::any_pdo_function
apokryfos
  • 38,771
  • 9
  • 70
  • 114