我正在尝试在数据库中插入数据,但是出现此错误,我不了解它是什么原因,因此需要您的帮助,请予以解决
try{
$fileName = $_FILES['fichier']['name'];
$files = '../save_Data/'.$fileName;
$csv = new SplFileObject($files); // On instancie l'objet SplFileObject
$csv->setFlags(SplFileObject::READ_CSV); // On indique que le fichier est de type CSV
$csv->setCsvControl(','); // On indique le caractère délimiteur, ici c'est la virgule
/**
* Préparation de la requête avec les paramètres.
*/
$nox = 'INSERT INTO public.nox_donnees_brutes (nox_date, nox_no_ppb, nox_no2_ppb, nox_nox_ppb) VALUES(:nox_date, :nox_no_ppb, :nox_no2_ppb, :nox_nox_ppb)';
$stm = $this->db->prepare($nox);
$stm->bindParam('nox_date', $nox_date, PDO::PARAM_STR);
$stm->bindParam('nox_no_ppb', $nox_no_ppb, PDO::PARAM_INT);
$stm->bindParam('nox_no2_ppb', $nox_no2_ppb, PDO::PARAM_INT);
$stm->bindParam('nox_nox_ppb', $nox_nox_ppb, PDO::PARAM_INT);
foreach ($csv as $line){
$character_sets = array("ISO-10646", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-9", "ISO-8859-10",
"ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16", "PC1250", "PC1251", "PC1252", "PC1253", "PC1254", "PC1255", "PC1256", "PC1257",
"PC1258", "Windows-1252", "UTF-8", "UTF-16", "UTF-32", "ASCII", "TOA5", "TOB1", "UTF-EBCDIC", "CESU-8", "BOCU-1");
$convert_files = mb_convert_encoding($line, 'UTF-8', $character_sets);
$nox_date = $convert_files[0];
$nox_no_ppb = $convert_files[1];
$nox_no2_ppb = $convert_files[2];
$nox_nox_ppb = $convert_files[3];
$stm->execute();
}
}
catch(PDOException $e)
{
die('Erreur lors de l\'insertion des données : Error ['. $e->getCode().'] ' . $e->getMessage() .'</p>');
}
答案 0 :(得分:0)
检查绑定变量是否为空/整数 testsetup:
$strQuery = "INSERT INTO public.tbl_test (test) VALUES (:PNVALUE)";
try {
$stm = $con->prepare($strQuery);
$stm->bindValue(':PNVALUE', $lsTest, PDO::PARAM_INT);
$stm->execute();
$arrResult = $loStatement->fetchAll();
}
catch(Exception $e) {
// do something
}
与:
$lsTest = '1'; //ok
$lsTest = 1; //ok
$lsTest = null; //ok
unset($lsTest); // 'PHP message: PHP Notice: Undefined variable: lsTest in ...
$lsTest = ""; // <- your error invalid input syntax for integer: “”
所以可以这样检查变量:
if(!is_int($lsTest)) {
$lsTest = NULL;
}
编辑:
您的代码应如下所示:
foreach ($csv as $line){
$character_sets = array("ISO-10646", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-9", "ISO-8859-10",
"ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16", "PC1250", "PC1251", "PC1252", "PC1253", "PC1254", "PC1255", "PC1256", "PC1257",
"PC1258", "Windows-1252", "UTF-8", "UTF-16", "UTF-32", "ASCII", "TOA5", "TOB1", "UTF-EBCDIC", "CESU-8", "BOCU-1");
$convert_files = mb_convert_encoding($line, 'UTF-8', $character_sets);
$nox_date = $convert_files[0];
$nox_no_ppb = $convert_files[1];
if(!is_int($nox_no_ppb )) {
$nox_no_ppb = NULL;
}
$nox_no2_ppb = $convert_files[2];
if(!is_int($nox_no2_ppb )) {
$nox_no2_ppb = NULL;
}
$nox_nox_ppb = $convert_files[3];
if(!is_int($nox_nox_ppb )) {
$nox_nox_ppb = NULL;
}
$nox = 'INSERT INTO public.nox_donnees_brutes (nox_date, nox_no_ppb, nox_no2_ppb, nox_nox_ppb) VALUES(:nox_date, :nox_no_ppb, :nox_no2_ppb, :nox_nox_ppb)';
$stm = $this->db->prepare($nox);
$stm->bindParam('nox_date', $nox_date, PDO::PARAM_STR);
$stm->bindParam('nox_no_ppb', $nox_no_ppb, PDO::PARAM_INT);
$stm->bindParam('nox_no2_ppb', $nox_no2_ppb, PDO::PARAM_INT);
$stm->bindParam('nox_nox_ppb', $nox_nox_ppb, PDO::PARAM_INT);
$stm->execute();
}