gpt4 book ai didi

PHP 为什么导入的csv文件只插入csv的第一行到SQL

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

假设 .csv 文件中有 3 行、3 列。在这种情况下,我需要导入完全相同的文件 3 次,才能将 .csv 文件中的所有数据完全插入到我的 SQL 数据库中。我认为 !feof($file) 应该确保循环到文件末尾?

if (isset($_POST['submitI'])){
$filename = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0){
$file = fopen($filename, "r");

while (!feof($file)){
$getData = fgetcsv($file, '0');

$sql = "SELECT * FROM murid WHERE No_Sek_Murid = '".$getData[0]."'";
$result = mysqli_query($conn, $sql);

if(empty($getData[0] || $getData[1] || $getData[2])){
$errorForm = "Sila isikan semua medan dalam fail!";
echo "<script type='text/javascript'>alert('$errorForm');</script>";
echo "<script> location.href='../main.php'; </script>";
exit();
}

if(mysqli_num_rows($result) === 0){
$sqlMurid = "INSERT INTO murid (No_Sek_Murid, Nama_Murid, Kelas_Murid) VALUES ('".$getData[0]."', '".$getData[1]."', '".$getData[2]."')";
$sqlKelab = "INSERT INTO kelab (Kod_Kelab, No_Sek_Murid, ID_Pengguna) VALUES ('{$_SESSION['kodKelab']}', '".$getData[0]."', '{$_SESSION['username']}')";
mysqli_query($conn, $sqlMurid);
mysqli_query($conn, $sqlKelab);

$success = "Berjaya menambah rekod ke dalam pangkalan data!";
echo "<script type='text/javascript'>alert('$success');</script>";
echo "<script> location.href='../main.php'; </script>";
exit();
}

} fclose($file);

} else {
$errorfile = "Operasi import gagal!";
echo "<script type='text/javascript'>alert('$errorfile');</script>";
echo "<script> location.href='../main.php'; </script>";
exit();
}

}

最佳答案

不必要时不要使用 exit()。此外,您还向empty() 函数传递了多个参数,这似乎不正确。您可以在没有退出功能的情况下运行代码,如下所示:

if (isset($_POST['submitI'])){
$filename = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0){
$file = fopen($filename, "r");

while (!feof($file)){
$getData = fgetcsv($file, '0');

$sql = "SELECT * FROM murid WHERE No_Sek_Murid = '".$getData[0]."'";
$result = mysqli_query($conn, $sql);

if(empty($getData[0]) || empty($getData[1]) || empty($getData[2])){
$errorForm = "Sila isikan semua medan dalam fail!";
echo "<script type='text/javascript'>alert('$errorForm');</script>";
echo "<script> location.href='../main.php'; </script>";
} elseif(mysqli_num_rows($result) === 0){
$sqlMurid = "INSERT INTO murid (No_Sek_Murid, Nama_Murid, Kelas_Murid) VALUES ('".$getData[0]."', '".$getData[1]."', '".$getData[2]."')";
$sqlKelab = "INSERT INTO kelab (Kod_Kelab, No_Sek_Murid, ID_Pengguna) VALUES ('{$_SESSION['kodKelab']}', '".$getData[0]."', '{$_SESSION['username']}')";
mysqli_query($conn, $sqlMurid);
mysqli_query($conn, $sqlKelab);

$success = "Berjaya menambah rekod ke dalam pangkalan data!";
echo "<script type='text/javascript'>alert('$success');</script>";
echo "<script> location.href='../main.php'; </script>";
}

} fclose($file);

} else {
$errorfile = "Operasi import gagal!";
echo "<script type='text/javascript'>alert('$errorfile');</script>";
echo "<script> location.href='../main.php'; </script>";
}

关于PHP 为什么导入的csv文件只插入csv的第一行到SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714025/

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