How to parse CSV file and store it's data into database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • witjian
    New Member
    • Mar 2014
    • 4

    How to parse CSV file and store it's data into database?

    I trying to store CSV data to database using onclick function. Unfortunately, I am using php code inside javascript function which is not efficient enough. Therefore, I hope that I can get any suggest or solution to improve efficiency of my project by using javascript instead of php to store CSV data into database.

    This is javascript with php code :
    Code:
    <script>
    function storeQueEmail(){
    <?php    
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 
    
        //loop through the csv file and insert into database 
        do { 
            if ($data[0]) { 
                $record['contact_first'] = $data[0];
                $record['contact_last'] = $data[1];
                $record['contact_email'] = $data[2];
                $record['subject'] = $_REQUEST['subject'];
                $record['message'] = $_REQUEST['message'];
                $record['status'] = 0;
    
                $oAdminEmail->insertQueEmail($record);
                } 
        } while ($data = fgetcsv($handle,1000,",","'")); 
    
        ?>  
        }
    </script>
    This is HTML code :
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <title>Import a CSV File with PHP & MySQL</title> 
    </head> 
    <body> 
    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" > 
      Subject : <br/>
      <input type="text" name="subject" id="subject" required/> <br/>
      Choose your upload type: <br /> 
        <input name="csv" type="file" id="csv" accept=".csv" required/> <br/>
      Content : <br/>
      <textarea name="message" cols="50" rows="10" required></textarea><br/>
      <input type="submit" name="submit" value="Submit" onclick="storeQueEmail()"/> 
    </form> 
    </body> 
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Unfortunately, I am using php code inside javascript function which is not efficient enough.
    that would be because PHP is a server-side language and JavaScript is a client-side language. just look at the webpage’s source code and you will see what your JavaScript function really contains.

    Comment

    • witjian
      New Member
      • Mar 2014
      • 4

      #3
      what can i do next? is it possible to provide me a sample which use javascript to store csv data into database?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        is it possible to provide me a sample which use javascript to store csv data into database?
        that would require JavaScript to have direct access to your DB, which is imossible for client-side JavaScript. what you have to do is let JavaScript collect the data send it over to a server script and let the server script insert the data into the DB.

        Comment

        • witjian
          New Member
          • Mar 2014
          • 4

          #5
          that's mean I cannot use javascript to get data from CSV file? beside javascript, can i use other language to get CSV data?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you can use JavaScript, but you must not confuse what is to happen on the server and what on the client.

            Comment

            • witjian
              New Member
              • Mar 2014
              • 4

              #7
              okay. i understand what my problem but can you provide me a sample code on how to read csv file by using javascript instead of using php? i don't want to pass my code to another new php and i just want to validate and do everything inside one page.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                ok, given you’d read your CSV with JavaScript, it still isn’t capable of saving the data in the DB.

                i don't want to pass my code to another new php
                the include command might be useful.

                Comment

                Working...