简体   繁体   English

使用html表单导入csv数据…使用PHP脚本加载数据infile和MySQL

[英]Using html form to import csv data… using PHP script load data infile and MySQL

I'm having trouble getting this script to work running on a MAMP local server: 我无法让此脚本在MAMP本地服务器上运行时遇到麻烦:

<?php
//Uploader Script for mass upload of devices.
//Check for errors
if($_FILES['file_upload']['error'] > 0){
    die('An error ocurred when uploading.');
}
//check file type is csv
if($_FILES['file_upload']['type'] != 'text/csv'){
    die('Unsupported filetype uploaded.');
}
//check size of csv
if($_FILES['file_upload']['size'] > 500000){
    die('File uploaded exceeds 500kb - maximum upload size.');
}
$file_url = $_FILES['file_upload']['tmp_name'];
$query = <<<eof
    LOAD DATA LOCAL INFILE '$file_url'
     REPLACE
     INTO TABLE DEVICE
     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
    (device_id, device_name, app_name)
eof;

$load_data = mysql_query($query);

The data is going into a single table with no foreign key dependencies. 数据将进入没有外键依赖性的单个表中。 I'm trying to upload a CSV and have been successfully using the database with other code. 我正在尝试上传CSV,并且已成功将数据库与其他代码一起使用。 I'm using this email form: 我正在使用此电子邮件表格:

<form action='upload.php' enctype='multipart/form-data'  method='post'>";
     File: <input type='file' name='file_upload'>"; 
     <input type='submit' name='Submit' value='Download Devices' />
 </form>

I think that you need to add enctype="multipart/form-data" to your form tag. 我认为您需要在表单标签中添加enctype =“ multipart / form-data”。

See What does enctype='multipart/form-data' mean? 请参阅enctype ='multipart / form-data'是什么意思?

OK so now we know that's in there, you need to check that the user running the MySQL process has permission to access the temporary file at $_FILES['file_upload']['tmp_name']. 好的,现在我们知道已经存在了,您需要检查运行MySQL进程的用户是否具有访问$ _FILES ['file_upload'] ['tmp_name']处的临时文件的权限。 This is the most likely point of failure. 这是最可能的故障点。

You might be able to find this using mysql_error or mysql_errno functions: http://php.net/mysql_error 您可能可以使用mysql_error或mysql_errno函数找到它: http : //php.net/mysql_error

$load_data = mysql_query($query)  or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); 

This will let you know if mysql failed and output why. 这将让您知道mysql是否失败并输出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM