gpt4 book ai didi

php - 使用 PHP 将数据从 csv 文件导入到 MySQL

转载 作者:行者123 更新时间:2023-11-30 22:52:55 25 4
gpt4 key购买 nike

我想将 CSV 文件中的数据插入到 MySQL 表中。为此,我现在使用以下代码,但是当我上传文件时,我的浏览器变得没有响应。几次后,弹出窗口显示重启 Firefox 或退出 Firefox。我只想知道,我在给定代码中的错误在哪里?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="imageup" /><input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
<?php
function generateRandomString($length = 10){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for($i = 0; $i < $length; $i++){
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}

if(isset($_POST['submit'])){
$t= generateRandomString();
$path = 'csv/';
$image = $_FILES["imageup"]["name"];
// $tmp = explode(".",$image);
$type = end($tmp);
$file = array("csv");
$csv_file = $path.$image;

if(in_array(strtolower($type), $file)){
if(move_uploaded_file($_FILES["imageup"]["tmp_name"], $path.$image)){
readfile($_FILES['imageup']['tmp_name']);

$open = fopen($_FILES['imageup']['tmp_name'], 'r');
$theData = fgets($open);
$i = 0;

while(!feof($open)){
$csv_data[] = fgets($open, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['ID'] = $csv_array[0];
$insert_csv['firstname'] = $csv_array[1];
$insert_csv['lastname'] = $csv_array[2];
$insert_csv['email'] = $csv_array[3];

$isql = "INSERT INTO `myguests`(`id`, `firstname`, `lastname`, `email`) VALUES ('','".$insert_csv['firstname']."','".$insert_csv['lastname']."','".$insert_csv['email']."')";
$run = mysqli_query($con, $isql);
$i++;
}
fclose($open);

echo "File upload successfully";
mysqli_close($con);
}
}
else{
echo "Not valid file formate";
}
}
?>

最佳答案

Please try this I have provide some php code for import data csv format 
<body>
<div id="container">
<div id="form">

<?php
$deleterecords = "TRUNCATE TABLE tablename"; //empty the table of its current records
mysql_query($deleterecords);

//Upload File
if (isset($_POST['submit'])) {

if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded
successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
readfile($_FILES['filename']['tmp_name']);
}

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$import="INSERT into importing(text,number)values('$data[0]','$data[1]')";

mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

//view upload form
} else {

print "Upload new csv by browsing to file and clicking on Upload<br />\n";

print "<form enctype='multipart/form-data' action='upload.php' method='post'>";

print "File name to import:<br />\n";

print "<input size='50' type='file' name='filename'><br />\n";

print "<input type='submit' name='submit' value='Upload'></form>";

}

?>

</div>
</div>
</body>

关于php - 使用 PHP 将数据从 csv 文件导入到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578739/

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