gpt4 book ai didi

PHP/MYSQL : Importing csv files through php into a mysql table

转载 作者:行者123 更新时间:2023-11-29 20:10:34 24 4
gpt4 key购买 nike

我一直在尝试从这个网站找到的代码:https://www.johnboyproductions.com/blog/tutorial-import-a-csv-file-using-php-and-mysql

它在本地非常适合我,但是当我在实时网站上尝试代码时。它说数据已导入但未反射(reflect)在数据库中。我添加以下代码:

if ($_FILES[csv][size] > 0) { 

//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");

//loop through the csv file and insert into database
do {
if ($data[0]) {
mysqli_query("INSERT INTO tablename VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//

//redirect
header('Location: filename.php?success=1'); die;

}

这是功能部分,我将在下面发布表单部分:

            <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>

任何帮助将不胜感激。谢谢

最佳答案

我尝试了最初使用的另一个代码,现在正在运行:

if(isset($_POST['Submit']))
{

if ($_FILES['csv']['size'] > 0)
{

//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");


$firstRow = true;

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{

if($firstRow) { $firstRow = false; }
else {

$str="INSERT INTO tablename VALUES
('' ,
'".addslashes($data[0])."',
'".addslashes($data[1])."'
) ";




$result = mysqli_query($connection,$str);
if($result)
$cnt++;
}
}

fclose($handle);

//redirect
// header('Location: filename.php?success=1'); die;

}


}

关于PHP/MYSQL : Importing csv files through php into a mysql table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40168734/

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