2

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

0

2 Answers 2

3

You need to call mysql_query to execute to query. Right now you only formulate the query as a string ($sql) and do nothing further. Just pass this string as a parameter, like:

$result = mysql_query($sql);

You may also want to handle any errors that occur. For instance (from PHP manual):

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

For details, see PHP manual on mysql_query.

Sign up to request clarification or add additional context in comments.

Comments

2

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    [email protected]
david   cotter    [email protected]
John    Painer    [email protected]
.......     ..........   ......................
.......     ..........   ......................

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.