1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need a script to upload and import csv file into mysql

Discussion in 'PHP' started by meldweny, Apr 25, 2010.

  1. #1
    hi all
    i need a script to upload csv to the server from local disk and then import data into mysql table
    i know its possible in phpmyadmin but i need a script to do that without login into cpanel
    thank you
     
    meldweny, Apr 25, 2010 IP
  2. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    Here's an article with well commented PHP code that should be easily adaptable to suit your purpose:

    http://www.codewalkers.com/c/a/Database-Code/Import-CSV-files-into-MySQL/

    Good luck with your project.

    Chipzzz
     
    Chipzzz, Apr 25, 2010 IP
  3. meldweny

    meldweny Active Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    thank you
    its very clear and straight but i still miss something
    i want to upload the file from local disk and this script needs that the file must be already found in the same directory with the same name of the table
     
    meldweny, Apr 25, 2010 IP
  4. alberrambo

    alberrambo Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $10 for all the solution
     
    alberrambo, Apr 25, 2010 IP
  5. meldweny

    meldweny Active Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    if you can help me in free your welcome
    thank you
     
    meldweny, Apr 25, 2010 IP
  6. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    Hi,

    Here is a tutorial from w3schools on uploading files with php:

    http://www.w3schools.com/PHP/php_file_upload.asp

    If you modify the first sections to accept csv files and replace the last section with a slightly modified version of the previous script, you should have no difficulty importing the uploaded file into your mysql database.

    I hope this helps,

    Chipzzz
     
    Chipzzz, Apr 25, 2010 IP
  7. meldweny

    meldweny Active Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #7
    i don`t know how to thank you
    but i try to do that and i failed , i am not php professional
    if you kindly join the two codes with needed modification this will be great
    Thank you in advance
     
    meldweny, Apr 25, 2010 IP
  8. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    Sorry, I thought you were trying to learn PHP. If you'd like the project written for you, Alberrambo will probably provide a tidier solution than the cobbled together code fragments, and his price seems quite reasonable. Sadly, there are demands on my time right now that would prevent me from doing it, and he did offer first.

    Best of luck with however you decide to pursue this,

    Chipzzz
     
    Chipzzz, Apr 25, 2010 IP
  9. meldweny

    meldweny Active Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    thank you
    i tried and searched and got it
    thats the code if you have any improvments or comments
    and also to help any one searching for that in the future
    
    <form enctype="multipart/form-data" action="1up.php" method="post">
    
    File to import:<br />
    
    <input size='30' type='file' name='filename'>
    
    <input type="submit" name="submit" value="Upload"></form>
    <?php 
    include "connect.php";
        //Upload File 
        if (isset($_POST['submit'])) { 
        if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
            echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; 
        $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
        $data = fgetcsv($handle, 1000, ","); 
        } 
         
        //Import uploaded file to Database 
        $row = 1; 
        $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
    
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
        if ( $row == 1){ 
        $row++; 
        } else { 
    
    
                  //Update Database Values 
    
            $import="insert into aaaa(name, report1, report2 ) 
            VALUES('".mysql_real_escape_string($data[0])."', 
                   '".mysql_real_escape_string($data[2])."', 
                   '".mysql_real_escape_string($data[3])."' )"; 
            mysql_query($import) or die(mysql_error()); 
            } 
        } 
    
    
        fclose($handle); 
         
    } 
        ?> 
    
    
    
    PHP:
     
    meldweny, Apr 25, 2010 IP
  10. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #10
    There are still some people in the world, who have got enough time to help other. Don't be so 'Sorry' and 'Sad' for these help seekers. You can try some Freelancer's site, where you get paid for your effort and time. Please don't make these free forums an Open market.


    Solution for the Thread Starter:

    
    
    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    File to import:<br />
    
    <input size='30' type='file' name='filename'>
    
    <input type="submit" name="submit" value="Upload"></form>
    
    
    
    <?php 
    //Assuming that, your connect.php file contains the database related information, ie. hostname, username etc.
    include "connect.php";
    
       //Upload File 
        if (isset($_POST['submit'])) { 
        if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
           
         
        //Import uploaded file to Database 
        $row = 1; 
        $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
    
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
       
    
    
                  //Update Database Values 
    
            $import="insert into test (name, data) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."')"; 
            mysql_query($import) or die(mysql_error()); 
         
        } 
    
    
        fclose($handle); 
         }
    } 
        ?>
    
    PHP:
    Remember to save the file with .PHP extension.
    As, you mentioned, you're not a professional, you may not understand the code completely. For that case, send me the exact mysql table schema. I'll modify the code accordingly.
     
    Last edited: Apr 26, 2010
    techbongo, Apr 26, 2010 IP
  11. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    Sorry, I'm new here and didn't mean to offend anyone.
     
    Chipzzz, Apr 26, 2010 IP
  12. meldweny

    meldweny Active Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #12
    techbongo
    Thank you very much and i am so pleased to know that there is somonr in this world who help others without asking for a price
    Chipzzz
    you make what you can do and helped as you can so thank you and welcome to this forum as new member

    techbongo
    its a chance for me and all other begainers to learn from you so,
    i see you make few changes on the code i posted
    please kindly can you explain for us the difference
    for example : you delete this line
    if ( $row == 1){ $row++; } else {

    why and what this line do exactly

    thank you very much all of you
     
    meldweny, Apr 26, 2010 IP
  13. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #13
    I see that you've been waiting for an answer for a while so let me chime in here, if nobody minds. Often csv files begin with a header line that contains information that you don't want added to the database when the file is imported. The lines you asked about prevented that header from being imported.

    Good luck with your project :)

    Chipzzz
     
    Chipzzz, Apr 26, 2010 IP
  14. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Chipzzz, Thank you for your understanding. I also never meant to hurt you, rather suggested some way that pays your effort. Cheers bro.

    @meldweny
    You're always welcome. Chipzzz explained exact reason for using the code snippet. I removed it because, in ideal cases, there won't be any column headers in your CSV. If you're sure that you've column headers in your csv, only then alter the code. However, you can customize the code to read the first row and identify if it's a column header or not. For that you may need to play with some regular expressions.(ie. to check, if the name field of the row contains the string, "Name").
     
    techbongo, Apr 27, 2010 IP
  15. Chipzzz

    Chipzzz Member

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #15
    Thanks, Techbongo. No harm done and thank you for the suggestion... I've been looking into it :) .
     
    Chipzzz, Apr 28, 2010 IP
  16. FreeGroup

    FreeGroup Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Hi,

    I don't like coding for such an "simple" task of import a Excel file. maybe the PHP script
    www.dbtube.org fits your requirements.

    Greetings
     
    FreeGroup, May 14, 2010 IP
  17. tascam424

    tascam424 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hi folks, i appreciate that this thread is quite old, but it answered most of what i needed, so rather than start a new thread ....
    Anyway, the above script, as amended by techbongo, is pretty much what i need, but i would like a small modification or direction as to how i may do it myself. I'm an absolute novice so please be gentle with me.
    The above script works well providing the table has already been created, but if it hasn't it obviously throws an error stating the table does not exist.
    What i would like is for it to look for the table and if it doesn't exist then create it .. so i had a search around and found this ...

    //Create Table
    
    
    CREATE TABLE IF NOT EXISTS users
    ( FirstName varchar(25), 
    SecondName varchar(25),
    Department varchar(25) NULL );
    
    Code (markup):
    I placed this snippet of code between //Upload File & //Import uploaded file to Database .. Obviously it doesn't work, but i have no idea where it is going wrong.
    I would appreciate any help along my tough learning curve.
    Thanks
     
    tascam424, Dec 23, 2012 IP