0

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

请帮我解决这个问题谢谢

不工作

  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

4 回答 4

1
mysql_query("INSERT INTO `sale2` (`username`, `date`, `item`, `quantity`, `amount`) VALUES 
                        (
                            '".trim($data[0], '"')."',
                            '".trim($data[1], '"')."', 
                            '".trim($data[2], '"')."',
                            '".trim($data[3], '"')."'
                        )
                    ");
于 2014-01-30T12:00:35.243 回答
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 文件,请看一下。

联系人.csv

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

最后一步连接数据库并运行下面的导入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> 
于 2014-02-11T04:02:57.617 回答
0

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

在这里,您将获得将 CSV 文件转换为集合数组的代码。 上传和读取 csv-file-in-php 的最佳方式

于 2014-05-15T08:42:15.760 回答
0

尝试找出您的脚本不工作的错误。还使用异常处理 try-catch 操作并通过 $e->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();
}
于 2013-11-08T07:37:52.950 回答