php 把csv文件導入到mysql數據庫 /* 本程序實現數據導入原理是先把csv文件上傳到服務器,然後再通過php的fopen與fgetcsv文件把數據保存到數組,然後再用while把數據一條條插入到mysql數據庫
php教程 把csv文件導入到mysql教程數據庫教程
/*
本程序實現數據導入原理是先把csv文件上傳到服務器,然後再通過php的fopen與fgetcsv文件把數據保存到數組,然後再用while把數據一條條插入到mysql數據庫
*/
$fname = $_files['myfile']['name'];
$do = copy($_files['myfile']['tmp_name'],$fname);
if ($do){
echo"導入數據成功<br>";
}else{
echo "";
}
error_reporting(0);// 導入csv格式的文件
$connect=mysql_connect("localhost","root","") or die("could not connect to database");
mysql_select_db("gklqtzcx",$connect) or die (mysql_error());
mysql_query("set names 'gbk'");
$fname = $_files['myfile']['name'];
$handle=fopen("$fname","r");
while($data=fgetcsv($handle,10000,",")){
$q="insert into records (name,classes,a_time,college,notify,receiver,r_time,handler) values ('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')";
mysql_query($q) or die (mysql_error());
}
fclose($handle);
echo "<meta http-equiv="refresh" content="1;url=list.php">1秒鐘轉入列表頁,請稍等."?>
<form enctype="multipart/form-data" action="<?php echo"".$_server["php_self"].""; ?>" method="post">
<p>導入cvs數據 <input name="myfile" type="file"> <input value="提交" type="submit">
</p>
</form>