Number of "bound variables" does not match the number of tokens - connection

0

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?

    
asked by anonymous 10.05.2018 / 16:52

2 answers

0

I found the solution for those who need it:

 $sql = "insert into {$table}" ."(" . implode(',',array_keys($fields)) .")"." values(" . ":". implode(',:',array_keys($fields)) .")";

 $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':name', $email);
    $stmt->bindParam(':name', $area);


$insert = $pdo->prepare($sql);

return $insert->execute($fields);

}

    
10.05.2018 / 20:28
0

In both cases in the line below must be used array_keys :

$sql = "insert into {$table} (" . implode(',',array_keys($fields)). ','."created_at" .") values(:".implode(',:'.array_values($fields)).",:". "$data_insert" .")";

Replace array_values with array_keys .

You can see the error in the query print itself:

'values(:gerente,:[email protected],:t,:2018-05-10)'

These are the values and not tokens.

Another point is that you are adding created_at separately. It should add to the field as well and remove it from the $ sql string:

$fields['created_at'] = date('Y-m-d');
$sql = "insert into {$table} "."(" . implode(',',array_keys($fields)). ') values(:'.implode(',:',array_keys($fields)).")";

Useful links for study:

PHP: Prepared Statements

    
10.05.2018 / 16:57