gpt4 book ai didi

php - CSV while 循环不循环

转载 作者:行者123 更新时间:2023-11-29 11:08:31 25 4
gpt4 key购买 nike

所以我想循环遍历 2 个文件(1 个 csv,1 个 asc),并且我想循环遍历这两个文件,以便它们都插入到数据库中,但不会导入它们。
这是代码:

<?php
function importdb() {
$File = 'lijst.csv';
$File2 = 'preise.asc';
$handle = fopen($File, "r");
$handle2 = fopen($File2, "r");
$arrResult = array();
$arrResult2 = array();
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
while(($data = fgetcsv($handle, 1000, ";")) !== FALSE || (($data2 = fgetcsv($handle2, 1000, ";"))) !== FALSE){
include('db_config.php');
$artikelnmr = $data[0];
$barcode = $data[1];
$omschrijving_nl = $data[2];
$omschrijving_exp = $data[3];
$bruto_prs = $data[4];
$staffel_prs = $data[5];
$aktie_prs = $data[6];
$bruto_antl = $data[8];
$staffel_antl = $data[9];
$aktie_aantal = $data[10];
$voorraad = $data[15];
$leverdatum = $data[16];
$besteld = $data[17];
$pallet_antl = $data[19];
$artikel_groep = $data[22];
$extra_info = $data[27];
$stmt = $db->prepare("INSERT INTO `producten`(`id`, `artikelnr`, `barcode`, `omschrijving_nl`, `omschrijving_exp`, `bruto_prijs`, `bruto_aant`, `staffel_prijs`, `staffel_aantal`, `aktie_prijs`, `aktie_aantal`, `voorraad`, `leverdatum`, `besteld`, `pallet_aantal`, `artikel_groep`, `extra`)
VALUES ('', :artikelnmr,:barcode,:omschrijving_nl,:omschrijving_exp,:bruto_prijs,:bruto_aantal,:staffel_prijs,:staffel_aantal,:aktie_prijs,:aktie_aantal,:voorraad,:leverdatum,:besteld,:pallet_aantal,:artikel_groep,:extra)");
$stmt->bindParam(":artikelnmr", $artikelnmr);
$stmt->bindParam(":barcode", $barcode);
$stmt->bindParam(":omschrijving_nl", $omschrijving_nl);
$stmt->bindParam(":omschrijving_exp", $omschrijving_exp);
$stmt->bindParam(":bruto_prijs", $bruto_prs);
$stmt->bindParam(":bruto_aantal", $bruto_antl);
$stmt->bindParam(":staffel_aantal", $staffel_antl);
$stmt->bindParam(":staffel_prijs", $staffel_prs);
$stmt->bindParam(":aktie_aantal", $aktie_aantal);
$stmt->bindParam(":aktie_prijs", $aktie_prs);
$stmt->bindParam(":voorraad", $voorraad);
$stmt->bindParam(":leverdatum", $leverdatum);
$stmt->bindParam(":besteld", $besteld);
$stmt->bindParam(":pallet_aantal", $pallet_antl);
$stmt->bindParam(":artikel_groep", $artikel_groep);
$stmt->bindParam(":extra", $extra_info);
$stmt->execute();
$stmt2 = $db->prepare("INSERT INTO `prijzen`(`artikelnr`, `prijs_soort`, `prijs1`, `prijs2`, `prijs3`, `prijs4`, `prijs5`) VALUES (:artikelnmr, :soort, :prijs1, :prijs2, :prijs3, :prijs4, :prijs5)");
$type = $data2[0];
$artikel = $artikelnmr;
$prijs1 = $data2[6];
$prijs2 = $data2[7];
$prijs3 = $data2[8];
$prijs4 = $data2[9];
$prijs5 = $data2[10];
$stmt2->bindParam("artikelnmr", $artikel);
$stmt2->bindParam(":soort", $type);
$stmt2->bindParam(":prijs1", $prijs1);
$stmt2->bindParam(":prijs2", $prijs2);
$stmt2->bindParam(":prijs3", $prijs3);
$stmt2->bindParam(":prijs4", $prijs4);
$stmt2->bindParam(":prijs5", $prijs5);
$stmt2->execute();
}
fclose($handle);
fclose($handle2);

}
importdb();
?>

当我运行代码时,它返回此错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'prijs_soort' cannot be null' in /home/ubuntu/workspace/import.php:65 Stack trace: #0 /home/ubuntu/workspace/import.php(65): PDOStatement->execute() #1 /home/ubuntu/workspace/import.php(71): importdb() #2 {main} thrown in /home/ubuntu/workspace/import.php on line 65

最佳答案

替换“||” while 条件中带有“&&”。

发生此错误的原因是其中一个文件已完成,而另一个文件尚未完成,但条件仍然为真,因为“或条件”之一为真。为了使代码正常工作,您需要两个条件都为真。

关于php - CSV while 循环不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40990484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com