PHP MySQL CSV文件导入页面

时间:2016-09-13 10:44:38

标签: php mysql csv

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

我有连接.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>";

}


?>

更新2:我试图从下面使用&#34; LOAD DATA INFILE&#34;以不同的角度来解决这个问题。但我还没到任何地方。首先,它不是创建一个表,但是与DB的连接正在运行。我不确定这种创建表的尝试是否失败然后显然会停止其余部分。欢迎思考!?!

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

}


?>

1 个答案:

答案 0 :(得分:0)

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

将它们分开并单独调用它们,或使用mysqli multi_query

您应该离开mysql*图书馆并进入mysqliPDO