Error "Invalid parameter number" when inserting records in table from CSV file

0

I want to read a CSV file that has three fields: cod, nom and amount; and insert them into a table that has the following columns:

TABLA
cod integer
nom varchar(45)
importe decimal(8,2)
periodo integer
fecha date

This is the code:

$registro = fopen($ruta.$formato_archivo, "r");
while (($data = fgetcsv($registro, 4096, ",")) !== FALSE) {
    $sth = $BD->prepare("INSERT INTO tabla (cod, nom, importe)
                VALUES (:cod, :nom, :importe)");
    $cod = $data[0];
    $nom = $data[1];
    $importe = $data[2];

    $sth->bindParam(':cod', $cod, PDO::PARAM_STR);
    $sth->bindParam(':nom', $nom, PDO::PARAM_STR);
    $sth->bindParam(':importe', $importe, PDO::PARAM_STR);

    $sth->execute();
}

And it shows me the error:

  

Invalid parameter number: number of bound variables does not match number of tokens in line (line of execute ()).

And it does not insert any record.

    
asked by Piropeator 25.03.2017 в 16:47
source

1 answer

0

Assuming you have echo var_dump($data) and everything has been correct.

Have you tried changing the PDO parameter that is passed to you?

$sth->bindParam(':cod', $cod, PDO::PARAM_INT);

With the Decimal type of "amount" when creating the table you may have problems. Check the TYPES of fields in the database and what you are inserting.

    
answered by 27.03.2017 в 23:54