我正在尝试将上传客户端的CSV文件插入我的MySQL数据库,但每次都失败; MySQL表仍然显示为空我对PHP / MySQL相当新,所以任何建议都会很棒!
HTML
<input type="file" name="file" id="file">
的JavaScript
function Upload_Text()
{
var formData = new FormData();
var file = document.getElementById("file").files[0];
formData = new FormData();
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var result = xmlhttp.responseText;
alert(result);
}
}
var url = "Upload_Text.php";
formData.append("thefile", file);
xmlhttp.open('POST', url, true);
xmlhttp.send(formData);
}
PHP
<?php
$file = $_FILES['thefile']['tmp_name'];
$link = new mysqli('address', 'usr', 'pwd', 'db');
if (mysqli_connect_error()) { echo "0"; die();}
$statement = $link->prepare("LOAD DATA INFILE ? INTO TABLE Groups FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';");
$statement->bind_param('s', $file);
$statement->execute();
?>
文件如下所示:
jakec,8
jim,5
debby,4
emily,8
terrence,2
感谢您的帮助
答案 0 :(得分:0)
改变了我的方法,并且能够使用以下方法:
<?php
$connect = mysql_connect('address', 'usr', 'pwd', 'db');
$handle = fopen($file,"r");
do {
if ($data[0]) {
mysql_query("INSERT INTO Students (NetID, GroupID) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
?>
虽然我知道这是用PHP 5.5.0折旧的,所以我不得不不可避免地改变它