如何使用php上传mysql数据库中的csv文件

时间:2013-11-08 07:24:02

标签: php mysql csv

这段代码工作得很完美但是当我添加两行时它不起作用

请帮我解决这个问题 感谢

不工作

  mysql_query("INSERT INTO sale2 (username, date, item, quantity, amount) VALUES

这是有效的

 mysql_query("INSERT INTO sale2 (username, date, item) VALUES 

这是完整的代码

<?php  

            //connect to the database 
            $connect = mysql_connect("localhost","root",""); 
            mysql_select_db("member",$connect); //select the table 
            // 

            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 sale2 (username, date, item) VALUES 
                            ( 
                                '".addslashes($data[0])."', 
                                '".addslashes($data[1])."', 
                                '".addslashes($data[2])."' 
            \

                            ) 
                        "); 
                    } 
                }  while ($data = fgetcsv($handle,1000,",","'")); 
                // 

                //redirect 
                header('Location: csv.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> 

4 个答案:

答案 0 :(得分:1)

mysql_query("INSERT INTO `sale2` (`username`, `date`, `item`, `quantity`, `amount`) VALUES 
                        (
                            '".trim($data[0], '"')."',
                            '".trim($data[1], '"')."', 
                            '".trim($data[2], '"')."',
                            '".trim($data[3], '"')."'
                        )
                    ");

答案 1 :(得分:0)

尝试查找脚本无法正常工作的错误。 还可以使用异常处理try-catch操作并通过$ e-&gt; getMessage();

获取消息
try {
   mysql_query("INSERT INTO sale2 (username, date, item) VALUES 
                        ( 
                            '".addslashes($data[0])."', 
                            '".addslashes($data[1])."', 
                            '".addslashes($data[2])."' 
        \

                        ) 
                    "); 
} catch (Exception $e) {
 echo $e->getMessage();
}

答案 2 :(得分:0)

首先在下面创建数据库是代码。

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `contacts`;
CREATE TABLE `contacts` (
  `contact_id` int(11) NOT NULL auto_increment,
  `contact_first` varchar(255) character set latin1 default NULL,
  `contact_last` varchar(255) character set latin1 default NULL,
  `contact_email` varchar(255) character set latin1 default NULL,
  PRIMARY KEY  (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

第二步创建了csv文件,请看一下。

contacts.csv

Jim,Smith,jim@tester.com
Joe,Tester,joe@tester.com

最后一步连接数据库并运行下面的import csv文件是代码。

 <?php  
$connect = mysql_connect("localhost","root",""); 
mysql_select_db("mydatabase",$connect);


if ($_FILES[csv][size] > 0) { 

    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 

    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 

    header('Location: index.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>"; } ?> 

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

答案 3 :(得分:0)

我认为您需要先在集合中转换CSV文件。之后,您可以添加循环和调用插入查询以插入数据。

在这里,您将获得将CSV文件转换为集合数组的代码。 best-way-to-upload-and-read-csv-file-in-php