gpt4 book ai didi

PHP MySQL CSV 文件导入页面

转载 作者:行者123 更新时间:2023-11-30 22:12:56 26 4
gpt4 key购买 nike

我正在尝试创建一个 CSV 文件 PHP 上传页面,它获取一个最终用户文件并将其上传到 mysql 数据库中的一个新表中。用户通过文本框指定表名,该值使用 session 存储,最终用户通过文件名输入选择 csv 文件。

我有 connection.php 工作,我已将下面的代码嵌入到页面中,但我一直在 mysql 查询中遇到语法错误。我可以一起执行这两个操作(即创建表和导入),还是需要单独执行?

欢迎任何想法..

<?php
session_start();

include "scripts/db_connection.php"; //Connect to Database

$_SESSION['tablename'] = $tablename;

$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="CREATE TABLE $tablename; INSERT into $tablename(item1,item2,item3,item4,item5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";

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_mobile.php' method='post'>";

print "Name of table to upload to:<br />\n";

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

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>";

}

?>

更新:好的,我已经使用了 mysqli 多查询,但我很困惑,因为页面/脚本运行没有错误,但没有创建基于 $tablename 的表或抛出错误。知道如何解决这个问题吗?

下面新更新的代码:

 <?php
session_start();

include "scripts/db_connection.php"; //Connect to Database

$_SESSION['tablename'] = $tablename;

$deleterecords = "TRUNCATE TABLE '$tablename'"; //empty the table of its current records
mysqli_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="CREATE TABLE $tablename; INSERT into $tablename(item1,item2,item3,item4,item5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";

mysqli_multi_query($import) or die(mysqli_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_mobile.php' method='post'>";

print "Name of table to upload to:<br />\n";

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

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>";

}


?>

UPDATE2:我尝试使用“LOAD DATA INFILE”从不同的角度来解决这个问题,但我仍然一无所获。首先,它没有创建表,但与数据库的连接正常。我不确定这种创建表的尝试是否失败,然后显然会停止其余部分。欢迎提出想法!?!

 <?php

session_start();

//connect to DB

$_SESSION['Tablename'] = $Tablename;
$_SESSION['filename'] = $filename;


}

if (isset($_POST['submit'])) {
$createtable = mysqli_query($db, "CREATE TABLE $Tablename");
mysqli_query($createtable) or die(mysqli_error());

$importfile = "
LOAD DATA INFILE '".$filename."'
INTO TABLE results CHARACTER SET utf8 FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES (name, description, price, shipping, quantity);
";

mysqli_query($importfile) or die(mysqli_error());

}

{

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

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

print "Name of table to upload to:<br />\n";

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

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>";

}


?>

最佳答案

这里有一个多查询。这意味着一个带有分号的字符串,然后是字符串中的其他语句。

要么将它们分开并单独调用它们,要么使用 mysqli multi_query .

无论如何,您应该离开 mysql* 库,转而使用 mysqliPDO

关于PHP MySQL CSV 文件导入页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39468117/

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