4

我正在将一个 CSV 文件导入我的管理区域,并且我想将文件添加到我的数据库中。我的 PHP 代码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;

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>

</body>
</html> 

代码正确显示,但是当我选择我的测试文件并点击提交时,我遇到了这些问题:

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 代码是这样的:

<?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

所以我的数据库连接没有问题。

谢谢你。

4

4 回答 4

1

您正在使用PDO建立数据库连接。但是$pdo实例不再使用了!

要执行一条SQL语句,需要使用like$pdo->query("INSERT INTO mobi ...")

相反,您mysql_query用于发送 MySQL 查询。因此,您会收到以下警告:

Access denied for user 'root'@'localhost' (using password: NO)

A link to the server could not be established

还要确保在header()删除此警告的函数之前没有输出任何字符串等headers already sent...

于 2013-08-27T16:47:03.007 回答
0

您的数据库凭据可能有误。检查服务器是否可访问。

并且已经发送的标头信息是由您的php代码之前的空格引起的

 <?php 

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

<?php 

include_once('../include/connection.php');
于 2013-08-27T16:01:13.043 回答
0

您的数据库凭据不正确。您还应该考虑对数据库使用mysqli面向对象的方法,并且应该使用mysqli_real_escape_string而不是添加斜杠。标头错误是 mysql 错误被抛出并输出到页面的结果。

于 2013-08-27T16:06:41.077 回答
0

您是否尝试过使用 php 加载文件,这样您就不必遍历它?

我建议使用require_once而不是include_once. __DIR__在包含路径之前也总是一件好事。这样您就可以说出文件来自当前文件的位置,而不是当前正在运行的脚本。

它看起来像这样:

<?php
require_once __DIR__ . '/../include/connection.php';

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

    //get the csv file
    $file = $_FILES[csv][tmp_name];

    mysql_query("
        LOAD DATA LOW_PRIORITY LOCAL 
        INFILE '$file'
        REPLACE INTO TABLE mobi 
        FIELDS TERMINATED BY ',' 
        OPTIONALLY ENCLOSED BY '\"' 
        ESCAPED BY '\"' 
        LINES TERMINATED BY '\n' 
        (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name)
        SET promo_name = TRIM(BOTH '\r' FROM promo_name);
    ");
}
于 2013-08-27T18:03:18.687 回答