Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:42:22+00:00 2026-05-26T20:42:22+00:00

I have the following code for uploading a csv file to a mysql database.

  • 0

I have the following code for uploading a csv file to a mysql database. Its working just fine, but if the csv file contains a heading for each column its getting uploaded in the first row of the table. I want to remove the first row of the csv file while getting stored in the database. How can i do that ?

<?php  

    //connect to the database 
    $connect = mysql_connect("localhost","root",""); 
    mysql_select_db("crm",$connect); //select the table 
    // 

    if ($_FILES[csv][size] > 0) { 

        //get the csv file 
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 

        //loop through the csv file and insert into database 
        do { 
            if ($data[0]) { 
                mysql_query("INSERT INTO lead (name, lead_value, status) VALUES 
                    ( 
                        '".addslashes($data[0])."', 
                        '".addslashes($data[1])."', 
                        '".addslashes($data[2])."' 
                    ) 
                "); 
            } 
        } while ($data = fgetcsv($handle,1000,",","'")); 
        // 

        //redirect 
        header('Location: import.php?success=1'); die; 

    } 

    ?> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <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> 

    <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> 

    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
      Choose your file: <br /> 
      <input name="csv" type="file" id="csv" /> 
      <input type="submit" name="Submit" value="Submit" /> 
    </form> 

    </body> 
    </html> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:42:23+00:00Added an answer on May 26, 2026 at 8:42 pm

    example code from what I’m using, and it should work because I’m using it in production environment

    $handle = fopen("stuff.csv","r");
    
    /* assuming that first row has collumns names */
    $query = "INSERT INTO table(";
    $cols = fgetcsv($handle,100000,';','"');
    $query .= implode(", ",$cols).") VALUES";
    
    $values = "";
    
        /* cycle through each row and build mysql insert query */
    while($data = fgetcsv($handle,100000,';','"')) {
        $values .= " ( ";
    
        foreach($data as $text) {
    
            $values .= "'".$text."', ";
    
        }
    
        $values = substr($values,0,-2);
    
        $values .= "), ";
    
    }
    
        /* remove last 2 chars */
    
    $values = substr($values,0,-2);
    
    $query .= $values;
    
    echo $query;
    
    fclose($handle);
    

    Mind the fact that this script will return the mysql query… not execute it, so alter it to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script on my server for uploading file. It is working fine
I have the following code for uploading images, when I tried to upload jpg
I have the following code for uploading a folder form local storage to blob
I have the following code: $('#imageform').submit(function() { $(#imageBox).html(''); $(#imageBox).html('<img src=img/loader.gif alt=Uploading..../>'); $(#imageform).ajaxSubmit( { target:
The following code is good for uploading text files, but it fails to upload
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.