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 などの他のページはすべて、connection.php の同じ include_once で動作することに注意してください。

データベース接続に問題はありません。

ありがとうございました。

4

4 に答える 4

0

データベースの認証情報が間違っている可能性があります。サーバーにアクセスできるかどうかを確認します。

すでに送信されたヘッダー情報は、php コードの前の空白が原因です

 <?php 

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

<?php 

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