使用php导入csv并更新mysql中的记录

时间:2015-09-11 04:23:46

标签: php mysql csv

我试图在php中导入一个csv文件并更新mysql数据库中的现有记录,但是我的代码只是没有工作,它没有任何错误,但它不会更新我的数据库。这是代码。

<?php  

$connect = mysql_connect("localhost","root",""); 
mysql_select_db("department",$connect); //select the table  

if (!empty($_FILES['csv']['size']) && ($_FILES['csv']['size']) > 0) {

get the csv file 
$file = $_FILES['csv']['tmp_name']; 
$handle = fopen($file,"r"); 

do { 
    if ($data[0]) { 
        mysql_query("INSERT INTO students (studno, lastname, firstname, mi,    
        sy, sem, course, deptname) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                '".addslashes($data[2])."',
                '".addslashes($data[3])."', 
                '".addslashes($data[4])."', 
                '".addslashes($data[5])."',
                '".addslashes($data[6])."',
                '".addslashes($data[7])."',
            ) 
        "); 
    } 
} while ($data = fgetcsv($handle,1000,",","'")); 

header('Location: admin/uploadinfo.php?success=1'); die; 
} 
?>

1 个答案:

答案 0 :(得分:1)

试试这个......

time