加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

通过PHP将CSV导入MYSQL

发布时间:2020-12-13 22:49:51 所属栏目:PHP教程 来源:网络整理
导读:我正在将CSV文件导入我的管理区域,我想将文件添加到我的数据库中.我对import.php的 PHP代码是: ?php include_once('../include/connection.php');if ($_FILES[csv][size] 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"
我正在将CSV文件导入我的管理区域,我想将文件添加到我的数据库中.我对import.php的 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

因为我的数据库连接没有任何关系.

谢谢.

解决方法

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

要执行SQL语句,需要像$pdo->查询一样使用(“INSERT INTO mobi …”)

相反,您正在使用mysql_query发送MySQL查询.所以你得到这些警告:

用户’root’@’localhost’拒绝访问(使用密码:否)

无法建立到服务器的链接

还要确保在header()函数之前没有输出任何字符串等…以删除此警告:已发送标头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读