天天看點

php将csv導入mysql_通過PHP将CSV導入MYSQL

我正在将CSV檔案導入我的管理區域,我想将檔案添加到我的資料庫中.我對import.php的

PHP代碼是:

include_once('../include/connection.php');

if ($_FILES[csv][size] > 0) {

//get the csv file

$file = $_FILES[csv][tmp_name];

$handle = fopen($file,"r");

//loop through the csv file and insert into database

do {

if ($data[0]) {

mysql_query("INSERT INTO mobi(promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES

(

'".addslashes($data[0])."',

'".addslashes($data[1])."',

'".addslashes($data[2])."',

'".addslashes($data[3])."',

'".addslashes($data[4])."',

'".addslashes($data[5])."'

)

");

}

} while ($data = fgetcsv($handle,1000,",","'"));

//

//redirect

header('Location: import.php?success=1'); die;

}

?>

Import a CSV File with PHP & MySQL

<?php if (!empty($_GET[success])) { echo "

Your file has been imported.

"; } //generic success notice ?>

Choose your file:

代碼顯示正确,但是當我選擇我的測試檔案并點選送出時,我遇到了以下問題:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in admin/import.php on line 23

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in admin/import.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at admin/import.php:1) in admin/import.php on line 29

有人能看出原因嗎?

我的connection.php代碼是這樣的:

try {

$pdo = new PDO('mysql:host=localhost;dbname=*********', '*********', '*********');

}catch (PDOException $e){

exit('Database Error.');

}

?>

請注意我的其他頁面,如add.php,delete.php,edit.php等都使用相同的include_once for connection.php

因為我的資料庫連接配接沒有任何關系.

謝謝.