我对php / mysql非常陌生但却陷入困境,创建了一个页面,允许我的用户上传csv文件并替换已更改的数据/插入新数据。我知道我的数据库连接正常工作,因为我有另一个页面以可排序/可搜索的方式显示数据库内容。以下是我得到的代码和错误。
dataimport.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>
dataimport.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行中包含了错误的文件,但是对于我的生活,我在这里的墙上敲打着我的头,找不到一个有用的例子。
添加db include
<?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();
}
?>
答案 0 :(得分:2)
'Access denied for user 'nocreport'@'localhost' (using password: YES)'
您的数据库连接配置错误。用户名或密码不匹配。
编辑。
在您添加更多源代码后进行了一些研究。
FILE
授予'nocreport'@'localhost'
权限。您还可以尝试添加LOCAL
这样的关键字:
LOAD DATA LOCAL INFILE'“。$ datafile。”'替换为表格字段终止于','终止于'\ n'IGNORE 1 LINES(@ col1,@ col2,@ col3 ...)