Why does not insert in PDO?

Created a database class
class Database
{
 private $link;
 public $title;
 public $content;
 protected $data;

 public function __construct()
{
 $config = require_once __DIR__.'/config.php';
 $dsn = 'mysql:host = '.$config['host'].';dbname='.$config['db_name'].';charset='.$config['charset'].";

try{
 $this->link = new PDO($dsn, $config['username'], $config['password']);
 }catch (PDOException $e){
 echo $e->getMessage();
}
}

 public function exec($title, $content){
 $stmt = $this->link->prepare("INSERT INTO test (title,content) VALUES (:title,:content)");
 $stmt -> execute(array(':title'=>$title, ':content'=>$content));
}
 }


later in index.php I want to insert data into the database
require_once __DIR__.'/Database.php';

 $insert = new Database();
 $insert->exec('new title', 'new content');


but it does not work, can not understand why.

And the selection works, here are the contents of the table

59d2205e13beb638942317.png

Thanks in advance.
June 14th 19 at 20:50
1 answer
June 14th 19 at 20:52
Solution
> Why does not insert in PDO?

Doesn't know anybody. In addition to your database.
So you need to ask need are not strangers, and its database.
Add after the connect line

$this->link->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


and read what she has to say.
Thanks for help, great article. - Luigi_K commented on June 14th 19 at 20:55

Find more questions by tags PHP PDO