通过PHP将CSV导入到MYSQL [英] importing CSV to MYSQL via PHP

查看:201
本文介绍了通过PHP将CSV导入到MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将CSV文件导入到我的管理区域,并且要将文件添加到我的数据库。我的 import.php 的PHP代码是:

I am importing a CSV file to my admin area and I want to add files to my database. My PHP code for import.php is:

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

代码显示正确,但是当我选择我的测试文件并点击submit时, :

The code displays correctly but when I select my test file and hit submit, I am hit with these problems:

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

任何人都能看到原因吗?

Can anyone see the reason?

my connection.php code is this:

my connection.php code is this:

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

please note that my other pages such as add.php, delete.php, edit.php and so on all work with the same include_once for connection.php

因此,我的数据库连接不再重要。

SO THERE IS NOTHINGWRONG WITH MY DATABASE CONNECTION.

谢谢。

推荐答案

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

You are using PDO to establish a database connection. But $pdo instance is not used anymore!

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

To executes an SQL statement, need to use like $pdo->query("INSERT INTO mobi ...")

而是使用 mysql_query 发送MySQL查询。因此您会收到以下警告:

Instead, You are using mysql_query to send a MySQL query. So you get these warnings:

用户'root'@'localhost'访问被拒绝(使用密码:NO)

无法建立指向服务器的链接

还要确保您在 header()函数之前不输出任何字符串等,以删除此警告:

Also make sure you are not output any strings, etc... before your header() function to remove this warning: headers already sent.

这篇关于通过PHP将CSV导入到MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆