想将上传的csv文件插入数据库。它说'没有选择数据库'我错过了什么?

时间:2014-03-21 12:24:50

标签: php mysql sql csv

<?php
    //connection to database
    include("sql/connect2DB.php"); 

    if (isset($_POST['submit'])) {
        if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
            echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
            echo "<h2>Displaying contents:</h2>";
            readfile($_FILES['filename']['tmp_name']);
        }

        //Import uploaded file to Database

        $handle = fopen($_FILES['filename']['tmp_name'], "r");

        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $import="INSERT into user(username,password, user_type) values('$data[0]','$data[1]', '$data[2]')";
            mysql_query($import) or die(mysql_error());
        }

        fclose($handle);

        print "Import done";

        //view upload form

    }
    else {
        print "Upload new csv by browsing to file and clicking on Upload<br />\n";
        print "<form enctype='multipart/form-data' action='test.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>";
    }

?>

1 个答案:

答案 0 :(得分:0)

您正在寻找http://uk1.php.net/mysql_select_db

应在连接完成后立即运行

虽然请注意页面上的大红色框,但您应该查看升级到Mysqli或PDO。