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 6747775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:29:03+00:00 2026-05-26T12:29:03+00:00

I have a simple scripts to upload csv file and it works . But

  • 0

I have a simple scripts to upload csv file and it works .
But how if I want to handle the data with headers specified?
Such as if my table have 3 columns: FirstName,LastName,Address
and now my csv file is:

Address,FirstName,LastName
myadd,myfname,mylname

How to insert the data correctly into mysql according to the headers even it’s not arrange same as table column name?

If I using my scripts , it will just insert myadd,myfname and mylname into FirstName,LastName and Address , even the headers will be inserted as well.

if (isset($_POST['upload'])) {

if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<p>contents:</p>";
    readfile($_FILES['filename']['tmp_name']);
}


$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $import="INSERT into CSVtest($column1,$column2,$column3,$column4,$column5) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";

    mysql_query($import) or die(mysql_error());
}

fclose($handle);

Echo "Done";

}

  • 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-26T12:29:04+00:00Added an answer on May 26, 2026 at 12:29 pm

    You say you have scripts : “If I using my scripts , it will just insert myadd,myfname and mylname into FirstName,LastName and Address , even the headers will be inserted as well.”

    I assume your scripts are generating the SQL to do this, namely you are saying INSERT INTO tablename VALUES(v1,v2,v3);

    instead of doing it that way, do a named insert
    INSERT INTO user (f1,f2,f3) VALUES(‘v1′,’v2′,’v3’);

    in other words specify the field names in the script that generates the sql.

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

Sidebar

Related Questions

I have written a simple file upload script but it gives me the error
I have a simple swf file upload script that uploads a file as soon
I have a very simple file upload PHP script. I am testing with a
I have this simple script which works fine, but currently overwrites files with duplicate
I have an system with which users can upload a CSV file via an
I have the below script to import data from a csv file on my
I have a few simple scripts that are intended to daisy chain together to
I have a simple Python script that I want to stop executing if a
I have a CSV file with names and addresses. I need to plot markers
I need to write a simple file transfer protocol in C++ to upload or

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.