0
<?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>";
    }

?>
AdRock
  • 2,959
  • 10
  • 66
  • 106
  • 3
    Damn, yeah, what can produce the error "No database selected".. Let me think about it.. Hmm.. – sunshinejr Mar 21 '14 at 12:28
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 21 '14 at 12:29
  • ... not mentioning that you forgot to include your connection code in your question. – ex3v Mar 21 '14 at 12:30

1 Answers1

0

Your looking for http://uk1.php.net/mysql_select_db

That should be run soon after the connection has been made

Though note the big red box on the page, You should look at upgrading to Mysqli or PDO.

exussum
  • 18,275
  • 8
  • 32
  • 65