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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:10:55+00:00 2026-06-13T17:10:55+00:00

I am trying to upload the contact data of csv to mysql database phbook

  • 0

I am trying to upload the contact data of csv to mysql database phbook using php,
but when i check through phpmyadmin or print the data of the database.. i dont see anything, or say the table is empty,

The code i used is as below;

    <?php
    /* conenction to DB */

    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("phbook", $con);

    /* connection ends*/

    if ( isset( $_FILES['userfile'] ) )
    {
      $csv_file = $_FILES['userfile']['tmp_name'];

      if ( ! is_file( $csv_file ) )
        exit('File not found.');

      $sql = '';

      if (($handle = fopen( $csv_file, "r")) !== FALSE)
      {
          while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
          {
              $sql .= "INSERT INTO `contact` SET
                `fname` = '$data[0]',
                `lname` = '$data[1]',
                `phone` = '$data[2]',
                `mob` = '$data[3]',
                `email` = '$data[4]';
              ";
          }
          fclose($handle);
      }

      // Insert into database

      //exit( $sql );
      exit( "Complete!" );
    }
    mysql_close($con);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <title>CSV to MySQL Via PHP</title>
    </head>
    <body>
    <form enctype="multipart/form-data" method="POST">
      <input name="userfile" type="file">
      <input type="submit" value="Upload">
    </form>
    </body>
    </html>

I have used the code the Import an excel (.csv) into MySQL using PHP code and an HTML form

  • 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-13T17:10:56+00:00Added an answer on June 13, 2026 at 5:10 pm

    For the help of others like me who are seeking for a solution, read this and follow,

    I created a database named csv, and table named test, where the 4 fields are id (auto increment and primary) , first_name, last_name, email. I have created a csv file and named it upload.csv(doesnt matter what you name it) where there are hundreds of data of individuals in 3 columns like First Name, Last Name and Email Address example:

    willie    walker    willie@example.com
    david   cotter    david@sometest.com
    John    Painer    john@domain.com
    …….     ……….   ………………….
    …….     ……….   ………………….

    Now, create a new php file import_csv_file.php(doesnt matter what you name it) and paste the codes below, save it and run under your webserver..Upload the csv file that we had created and see.. IT WORKS

        <?php
    
        $con = mysql_connect("localhost","root","") or die (mysql_error());
    
        mysql_select_db('csv', $con);
    
            if(isset($_POST['submit']))
            {
                $file = $_FILES['file']['tmp_name'];
                $handle = fopen($file,"r");
                while(($fileop = fgetcsv($handle,1000,",")) !==false)
                {
    
                    $f = $fileop[0];
                    $l = $fileop[1];
                    $e = $fileop[2];
                    $sql= mysql_query("INSERT INTO test (first_name,last_name,email) VALUES ('$f','$l','$e')");
                }
                if($sql)
                {
                    echo "data uploaded successfully";
                }
            }
    
        ?>
    
        <body>
        <form action="" enctype="multipart/form-data" method="post">
          <input type="file" name="file" />
          <br />
          <input type="submit" value="Submit" name="submit"/>
        </form>
        </body>
        </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to upload CSV data to MYSQL table using the below query.It's running
I'm trying to upload images to a php file, everything works, but sometimes when
trying to upload a file on a facelets page using Tomahawk2.0 1.1.11. But I
I'm trying to add a file upload function to a contact form, but it
Im trying to upload some images using the Flickr.net API.The Images are uploaded but
im trying to upload a file using org.apache.commons.fileupload. but i dont no, what mistake
I'm trying to upload file, using XMLHTTPRequest, and sending this headers: Content-Type:multipart/form-data, boundary=xxxxxxxxx --xxxxxxxxx
I am trying to upload a folder (and its content) through FTP but when
I'm trying to upload a presentation, using the google api drive, but the presentation
So I'm trying to upload a document to a sharepoint site using a Java

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.