0

我对 php/mysql 非常陌生,但在创建允许我的用户上传 csv 文件并替换更改的数据/插入新数据的页面时遇到了困难。我知道我的数据库连接有效,因为我有另一个页面以可排序/可搜索的方式显示数据库内容。下面是我得到的代码和错误。

数据导入.html

<html>
<body>

<form action="dataimport.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

数据导入.php

<?php
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  exit();
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

$datafile = $_FILES["file"];

  try
{
  $sql = "LOAD DATA INFILE'".$datafile."' REPLACE INTO TABLE table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES
    (@col1,@col2,@col3...)";
  $s = $pdo->prepare($sql);
  $s->execute();
}
catch (PDOException $e)
{
  $error = 'Unable to connect to the database server.';
  include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  exit();
}
?>

我得到的输出:

Upload: Report.csv
Type: application/vnd.ms-excel
Size: 1010.1943359375 kB
Stored in: E:\XAMPP\tmp\phpBD04.tmp
Notice: Array to string conversion in \dataimport.php on line 21

Unable to connect to the database server.
exception 'PDOException' with message       'SQLSTATE[28000]: Invalid authorization specification: 1045 Access denied for user 'user'@'localhost' (using password: YES)' in \dataimport.php:24 Stack trace: #0 \dataimport.php(24): PDOStatement->execute() #1 {main} 

我知道我可能在 $sql 行中包含了错误的文件,但是对于我的一生,我在这里一直在敲打我的头,找不到一个有用的例子。

添加数据库包括

<?php
try
{
  $pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'pass');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
  $error = 'Unable to connect to the database server.';
  include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
  exit();
}
?>
4

1 回答 1

2
'Access denied for user 'nocreport'@'localhost' (using password: YES)'

您有错误的数据库连接配置。用户名或密码不匹配。

编辑。

添加更多源代码后进行了一些研究。

  1. 检查您是否已FILE授予用户权限'nocreport'@'localhost'
  2. 您也可以尝试LOCAL像这样添加关键字:

    LOAD DATA LOCAL INFILE '".$datafile."' REPLACE INTO TABLE table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (@col1,@col2,@col3...)

于 2013-06-18T17:50:01.300 回答