Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload CSV and Insert into Database Using PHP/MYSQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 106
    Comment on it

    Hello Readers !

    We can simply upload upload CSV data into database by two simple ways.

    Step 1 Data Base Connection

    <?php
    $db = mysq_connect("Database", "username", "password") or die("Could not connect.");
    
    if(!$db) 
    
        die("no db");
    
    if(!mysql_select_db("Databasename",$db))
    
         die("No database selected.");
    ?>
    

    Step 2 Simply select Upload File

    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 tablename(item1,item2,item3,item4,item5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
    
            mysql_query($import) or die(mysql_error());
        }
    
        fclose($handle);
    
        print "Import done";
    
        //view upload form
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: