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

  • Home
  • SEARCH
  • 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 8405923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:57:27+00:00 2026-06-09T22:57:27+00:00

I am currently working on an CSV Uploader project and I have it working

  • 0

I am currently working on an CSV Uploader project and I have it working correctly but my question is – How would I make it select (firstname, lastname, email, phone, etc) if clients CSV file is put together differently. Or would the client who is uploading the file have to have it in the order the program is made?

Here is php code ———————————————-

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}

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

    $file = $_FILES['files']['tmp_name'];   

    $handle = fopen($file , "r");

    while (($fileop = fgetcsv($handle,1000,",")) !== false)
    {
        $firstname = $fileop[0];
        $lastname = $fileop[1];
        $email = $fileop[2];
        $phone = $fileop[3];

        $insertdata = $DBH->prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
        $insertdata->execute();
    }
    if ($insetdata){
        echo "Successfully Uploaded. Thank you.";   
    }
}

Thank you for your time!

UPDATED———————————————-

New code

   try {  
      # MySQL with PDO_MYSQL  
      $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
      $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }  
    catch(PDOException $e) { 
        echo "I'm sorry, I'm afraid I can't do that.";  
        file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
    }

    function returnBack(){

        header("Location:http://pcgprotects.com/PCG/csvuploader/csvuploaderform.php?seterror=1");
        exit;

    }

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

        /*******************************GET NAME OF COLUMNS IN CSV FILE *********************************************************/
        if (empty($_POST['files'])){
            returnBack();
        }
        if (empty($_POST['firstname'])){
            returnBack();
        }
        if (empty($_POST['lastname'])){
            returnBack();
        }if (empty($_POST['email'])){
            returnBack();
        }
        if (empty($_POST['phone'])){
            returnBack();
        }
$file = $_FILES['files']['tmp_name'];   

        $handle = fopen($file , "r");
        $fileop = fgetcsv($handle,1000,",");
        $firstname_index = array_search($_POST['firstname'],$fileop);
        if (empty($firstname_index)){
            returnBack();
        }
        $lastname_index = array_search($_POST['lastname'],$fileop);
        if (empty($lastname_index)){
            returnBack();
        }
        $email_index = array_search($_POST['email'],$fileop);
        if (empty($email_index)){
            returnBack();
        }
        $phone_index = array_search($_POST['phone'],$fileop);
        if (empty($phone_index)){
            returnBack();
        }

        /***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEN INTO CSV TABLE IN DB *************************************/


        while (($fileop = fgetcsv($handle,1000,",")) !== false)
        {
            $firstname = $fileop[$firstname_index];
            $lastname = $fileop[$lastname_index];
            $email = $fileop[$email_index];
            $phone = $fileop[$phone_index];

            $insertdata = $DBH->prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
            $insertdata->execute();
        }
        if ($insetdata){
            echo "Successfully Uploaded. Thank you.";   
        }
    }

In my csv trial file i used columns named – First name, Last name, Phone, Email. Then in my form I entered those values but I got kicked back – here is my Form code —–

    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form method="post" action="csvuploader.php" enctype="multipart/form-data">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>CSV Uploader </strong></td>
</tr>
<tr>
<td>Enter Name of First Name Column </td>
<td>:</td>
<td><input name="fisrtname" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){
    echo "<span class='errorMsg'>Need Exact Name</span>";
    }
        ?> </td>
</tr>
<tr>
<td>Enter Name of Last Name Column </td>
<td>:</td>
<td><input name="lastname" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){
    echo "<span class='errorMsg'>Need Exact Name</span>";
    }
        ?></td>
</tr>
<tr>
<td>Enter Name of Email Column </td>
<td>:</td>
<td><input name="email" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){
    echo "<span class='errorMsg'>Need Exact Name</span>";
    }
        ?></td>
</tr>
<tr>
<td>Enter Name of Phone Number Column </td>
<td>:</td>
<td><input name="phone" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){
    echo "<span class='errorMsg'>Need Exact Name</span>";
    }
        ?></td>
</tr>
<tr>
<td width="294"><input type="file" name="files" /><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){
    echo "<span class='errorMsg'>Select a File</span>";
    }
        ?></td>
</tr>
<br />
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
  • 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-06-09T22:57:28+00:00Added an answer on June 9, 2026 at 10:57 pm

    You can ask the customer to add one line in the beginning of the CSV that will show the structure, for example:

    firstname,lastname,email,phone
    joe,smith,j@gmail.com,41595648855
    joe2,smith2,j2@gmail.com,41595648852
    

    …

    from reading the first line – you’ll be able to know which token you should use with which index.

    $file = $_FILES['files']['tmp_name'];   
    $handle = fopen($file , "r");
    $fileop = fgetcsv($handle,1000,",");
    $firstname_index = array_search("firstname",$fileop);
    $lastname_index = array_search("lastname",$fileop);
    ...
    //and then
    while (($fileop = fgetcsv($handle,1000,",")) !== false)
    {
      $firstname = $fileop[$firstname_index];
      $lastname = $fileop[$lastname_index];
      ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project where users can upload datasets in CSV format.
I'm currently working on a project that involves a CSV file. I initially was
I am currently working on a computer science project where I have to evaluate
Edit: solution added. Hi, I currently have some working albeit slow code. It merges
I've started working on some Android applications and have a question regarding how people
I am currently working on a project for traversing an excel document and inserting
I'm currently working on a project that is done in Java, on google appengine.
I'm working with a CSV file in python, which will have ~100,000 rows when
I have created a solution which read a large csv file currently 20-30 mb
I am coding multiple annotations into a project. Currently I have 30 annotations, and

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.