www.webdeveloper.com
Results 1 to 4 of 4

Thread: import csv file in php

  1. #1
    Join Date
    Apr 2014
    Posts
    10

    import csv file in php

    hey guys so ive been asked to add an import csv button/function to my contacts page

    i found this coding on the net and just want some clarification because there was no explanation,if you guys don't mind.

    Code:
    <?php  
    require "connection.php";
    if ($_FILES[csv][size] > 0) { 
        //get the csv file 
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 
            if ($data[0]) { 
                mysql_query("INSERT INTO contact (fname,lname,email) VALUES 
                    ( 
                        '".addslashes($data[0])."', 
                        '".addslashes($data[1])."', 
                        '".addslashes($data[2])."' 
                    ) 
                "); 
            } 
        } while ($data = fgetcsv($handle,1000,",","'")); 
    } 
    ?>
    fopen reminds me of ubuntu terminal codes back in uni.

    1. if($data[0]) checks if the first "column" of a csv file exists?
    2. stumbled on a site that said addslashes isnt good but did not elaborat. why isnt it good?
    3. say my first column is id and its auto-increment do i add column id in the insert query? -> contact (id,fname,lname,email)
    4. what if the csv file has header? how to code to check if theres header and if there is ignore the header.
    5. whats the 1000 in the fgetcsv($handle,1000,",","'"))?

    thanks in advance!

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    562
    Throw it away and keep searching for some workable code. This one does the query to insert a record before it even retrieves a row of data from the csv file. Plus - it uses non-standard notation which I think you should not use since it will probably bite you someday (indices s/b in quotes!)

    Do you program in any other language, because it s/b obvious that this code is backwards.

  3. #3
    Join Date
    Apr 2014
    Posts
    10
    just php and html for now.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Quote Originally Posted by noobdvlpr View Post
    ...

    1. if($data[0]) checks if the first "column" of a csv file exists?
    yes, sort of: if the first column happened to be a 0 (zero) or empty string (""), it would consider it to be false, so might not always be appropriate. Assuming I know the number of fields I should be receiving, I prefer to check for that:
    Code:
    $numFields = 5;
    if(count($data) == $numFields) {
    2. stumbled on a site that said addslashes isnt good but did not elaborat. why isnt it good?
    you should use the escaping mechanism specific to the database extension you are using -- in this case mysql_reql_escape_string() for the mysql extensions -- which is deprecated and should not be used in new applications (use PDO or MySQLi instead)
    3. say my first column is id and its auto-increment do i add column id in the insert query? -> contact (id,fname,lname,email)
    depends on whether you need to carry that ID over from the source to the target or not (and if you do, then you have to figure out how to handle duplicates)
    4. what if the csv file has header? how to code to check if theres header and if there is ignore the header.
    You either need to decide/specify up front that there will or will not always be a header, or else provide a field on the form input for the user to specify whether it is there (or do some analysis of the field values and hope you guess right)
    5. whats the 1000 in the fgetcsv($handle,1000,",","'"))?
    Easiest way to answer that sort of question: enter "php.net/" plus the function name into your browser, e.g. http://php.net/fgetcsv

    But yeah, that code is pretty weak, and even if you fixed the while() loop (http://php.net/while ) to work, at a minimum, it would still throw a bunch of notices/warnings, and is not very robust.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles