Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\trofeugs2017\model\Model.php on line 81
How to fix the error?
Code:
<?php
class Model {
protected $name_table;
protected $fields;
protected $id_row;
public $con;
public function __construct($name_table){
$this->name_table = $name_table;
$this->con = new PDO("mysql:host=localhost;dbname=trofeu2017", "root", "");
}
function implodeFields($fields){
return implode(', ', $fields);
}
function implodeKeys($fields){
return implode(', ', array_keys($fields));
}
function implodeValues($fields){
return "'" . implode("', '", array_values($fields)) . "'";
}
public function updateTable(array $fields, $where){
if($where){
$where = "WHERE {$where}";
} else {
$where = null;
}
foreach($fields as $key => $values){
$campos[] = "'{$key}' = '{$values}'";
}
$fields = $this->implodeFields($campos);
$sql = "UPDATE {$this->name_table} SET {$fields} {$where}";
$update = $this->con->prepare($sql);
if($update->execute()){
return $update;
} else {
return false;
}
}
}