Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Location
    Pakistan
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting Excel Sheet data into Mysql; does not work

    Hello,

    I am trying to insert excel sheet data into mysql database. First, I convert that file into comma delimited CSV and then apply this code.

    PHP Code:
    $link mysql_connect("localhost""user""pass");
    mysql_select_db("db");     

    $file $_FILES['excel_file']['name'];
    @
    copy($_FILES['excel_file']['tmp_name'], "files/$file");
    $query 'LOAD DATA INFILE /www/trisha/admin/"'.$file.'" INTO TABLE student_info FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";';

    mysql_query($query);
    @
    unlink($file); 
    My purpose is
    1; upload csv file
    2; run load command and insert that file data into DB
    2; remove that uploaded file

    Please advise what I am doing mistake.

    TIA!
    God helps those who help others.

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What kind of error or warning did you recieve when you try to insert csv file
    If you can not find a decision, maybe you have to try to change a problem

    http://www.gunman.co.nr
    http://bglinux.org

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The path where the uploaded file is copied to is something like current_folder_this_script_is_in/files/ The path that is being put into the query should at least end in .../files/ if it is referring to the same location.

    The copy(...) statement has error output suppressed with an @ sign. The upload could be failing, which would cause the copy to fail, or the copy could be failing due to a missing folder, or a folder/file permissions problem. The code needs to check for errors as the result of the upload (did the file get uploaded) and check if the copy worked, before attempting to load the file into the database.

    Only the .$file. portion of the path/file in the query is surrounded with quotes. The whole path/file needs to be within quotes. From the mysql manual -
    LOAD DATA [LOCAL] INFILE 'file_name' INTO TABLE tbl_name ...
    Without the LOCAL keyword, the file must be placed within the mysql server's file system and be directly accessible and readable by the mysql server. If this is not the case, you need to use the LOCAL keyword to get this to be able to read the file from where it is accessible by PHP.

    The mysql query has no error checking and reporting -
    PHP Code:
    mysql_query($query) or die('Query Failed: ' mysql_error()); 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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