gpt4 book ai didi

php - 如何使用php将csv文件上传到mysql?

转载 作者:行者123 更新时间:2023-11-29 12:43:21 26 4
gpt4 key购买 nike

如何使用php正确上传csv文件到mysql数据库?

我在phpmyadmin中有一个列(已上传,列名称),在此列中将存储上传的csv文件。

我有一个将 csv 文件上传到 mysql 数据库的小脚本,但在单击提交按钮后出现一些错误“404:找不到文件”,并且 csv 文件未保存在数据库中。

这里是带有 html 的 php 文件:

<?php
include "dbase.php"; //Connect to Database
$deleterecords = "TRUNCATE TABLE contracts"; //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 contracts(uploaded) values('$data[0],$data[1]')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
echo "Import done";
//view upload form
}else {
echo "Upload new csv by browsing to file and clicking on Upload<br />\n";
echo "<form enctype='multipart/form-data' action='upload.php' method='post'>";
echo "File name to import:<br />\n";
echo "<input size='50' type='file' name='filename'><br />\n";
echo "<input type='submit' name='submit' value='Upload'></form>";
}
?>

数据库连接:

 <?
$filename=$_POST['filename'];
$db=mysql_connect('localhost', 'username', 'password', '3306')
or die(mysql_error());
if(!$db)
die("no db");
if(!mysql_select_db("databasename",$db))
die("No database selected.");
?>

最佳答案

我认为您的脚本文件的名称不是 upload.php,这就是您收到“Not Found”错误的原因。

关于php - 如何使用php将csv文件上传到mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25800305/

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