Fatal error: Uncaught PDOException: SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C: \ xampp \ htdocs \ page-access \ function \ database.php: 48 Stack trace: # 0 C: \ xampp \ htdocs \ page-access \ function \ database.php (48): PDOStatement-> execute (Array) # 1 C: \ xampp \ htdocs \ page-access \ pages \ forms \ .php (31): create ('manager', Array) # 2 C: \ xampp \ htdocs \ page-access \ vendor \ composer \ autoload_real.php (66): require ('C: \ xampp \ htdocs ... ') # 3 C: \ xampp \ htdocs \ page-access \ vendor \ composer \ autoload_real.php (56): composerRequired907de9795a329bdba4a92f56fc2a475 (' efb971a9ff2a384 ... ',' C: \ xampp \ htdocs ... ') # : \ xampp \ htdocs \ page-access \ vendor \ autoload.php (7): ComposerAutoloaderInitd907de9795a329bdba4a92f56fc2a475 :: getLoader () # 5 C: \ xampp \ htdocs \ \ xampp \ htdocs ... ') # 6 C: \ xampp \ htdocs \ page-access \ pages \ forms \ cadastro-gerente.php (12): require (' C: \ xampp \ 7 {main} thrown in C: \ xampp \ htdocs \ page-access \ function \ database.php on line 48
What I want to do: Add a manager to my manager table.
function connect(){
$config =
[
'db' =>[
'host' => 'localhost',
'dbname' => 'crobi_solicitacao',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
]
];
$pdo = new \PDO("mysql:host={$config['db']['host']};dbname={$config['db']['dbname']};charset={$config['db']['charset']}",$config['db']['username'],$config['db']['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ); //para trabalhar como objeto.
return $pdo;
}
function criar($table, $fields){
// para cadastrar no BD precisa estar na forma de array
if(!is_array($fields)){
$fields = (array) $fields;
}
//PEGO A DATA PARA O MEU CAMPO
$data_insert = date('Y-m-d');
$sql = "insert into {$table}" ."(" . implode(',',array_keys($fields)). ','."created_at" .")"." values(". ":".implode(',:',array_values($fields)).",:". "$data_insert" .")";
$pdo = connect();
$insert = $pdo->prepare($sql);
return $insert->execute($fields);
}
To find out how I got a dd ($ sql), I got the following result:
string(114) "insert into gerente(name,email,area,created_at) values(:gerente,:[email protected],:t,:2018-05-10)"
What do I need to change to do the insertion?