将CSV文件放在MYSQL数据库中不起作用

时间:2015-01-07 09:15:33

标签: php mysql csv

我正在网站上学,我需要在csv数据库中使用mysql上传php个文件。 我不能简单地将它上传到phpMyAdmin。

现在我正在为我做一些代码。但是代码并没有完成将我的CSV文件放入mysql数据库。我已经阅读了人们的其他几个问题,但那里的答案并没有帮助我

我有两个文件,一个是

connection.php

$dbName = "i296297_studie";
$user = "username";
$password = "password";
$host = "localhost";

$connection = mysqli_connect($host, $user, $password, $dbName);

if (mysqli_connect_errno()) 
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

这很好,一个是 test1.php

include "connection.php"; //connectie database
$deleterecords = "TRUNCATE TABLE olympischespelen"; //haalt de tabel leeg
mysqli_query($connection, $deleterecords);


//wanneer file is geupload 
if (isset($_POST['submit'])) {
    $csv_file = $_FILES['filename']['tmp_name'];
    if (($getfile = fopen($csv_file, "r")) !== FALSE) { 
        $data = fgetcsv($getfile, 1000, ";");
        while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
         $num = count($data); 
         for ($c=0; $c < $num; $c++) {
             $result = $data; 
             $str = implode(";", $result); 
             $slice = explode(";", $str);
             $col1 = $slice[0]; 
             $col2 = $slice[1];
             $col3 = $slice[2];
             $col4 = $slice[3];
             $col5 = $slice[4];
             $col6 = $slice[5];

// SQL Query to insert data into DataBase

$query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`) 
VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";

        mysqli_query($query, $connection); 
     }
   } 
  }

echo "File data successfully imported to database!!"; 
mysqli_close($connect); 

}else {

    print "Upload new csv by browsing to file and clicking on Upload<br />\n";
    print "<form enctype='multipart/form-data' action='test1.php' method='post'>";
    print "File name to import:<br />\n";
    print "<input size='50' type='file' name='filename'><br />\n";
    print "<input type='submit' name='submit' value='Upload'></form>";

}

?>

但我不知道哪里出错了。当我回复$col1或其他人时,他们正在输出我想要的数据。

希望你能帮助我谢谢

3 个答案:

答案 0 :(得分:1)

<?php  

//connect to the database 
$connect = mysql_connect("localhost","username","password"); 
mysql_select_db("mydatabase",$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 contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 
    // 

    //redirect 
    header('Location: import.php?success=1'); die; 

} 

?>

<!DOCTYPE html> 
<head>
<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> 

看,如果这对你有所帮助。

答案 1 :(得分:1)

您将此fgetcsv($getfile, 1000, ";");称为两次。

试试这个

if (($getfile = fopen($csv_file, "r")) !== FALSE) { 
        while (($data = fgetcsv($getfile, 1000, ";")) !== FALSE) {
            $num = count($data); 
            for ($c=0; $c < $num; $c++) {
                $result = $data; 
                $str = implode(";", $result); 
                $slice = explode(";", $str);
                $col1 = $slice[0]; 
                $col2 = $slice[1];
                $col3 = $slice[2];
                $col4 = $slice[3];
                $col5 = $slice[4];
                $col6 = $slice[5];

                // SQL Query to insert data into DataBase

                $query = "INSERT INTO `i296297_studie`.`olympischespelen` (`sport`, `deelnemers`, `goud`, `zilver`, `brons`, `totaal`) 
                VALUES ('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."')";

                mysqli_query($query, $connection); 
            }
        } 
    }

在插入db ..

之前,所有数据都按mysql_real_escape_string过滤

答案 2 :(得分:0)

您需要将连接链接作为第一个参数传递

mysqli_query($query, $connection); 

应该是

mysqli_query($connection, $query);