Jump to content

uploading a csv file into mysql database


xclusivzik

Recommended Posts

when i input this code it only uploads a single column into  my database

<html>
<head>
    <title>MySQL file upload example</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="try2.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file"><br>
        <input type="submit"  name="submit"value="Upload file">
    </form>
    <p>
        <a href="list_files.php">See all files</a>
    </p>
</body>
<?php
$con=mysqli_connect("localhost","root","","book");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


if(isset($_POST['submit']))
{
     $fname = $_FILES['uploaded_file']['name'];
     
     $chk_ext = explode(".",$fname);
     
     if(strtolower($chk_ext[1]) == "csv")
     {
     
         $filename = $_FILES['uploaded_file']['tmp_name'];
         $handle = fopen($filename, "r");
    
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
         {
            $sql = "INSERT into data(name,Groups,phone_number) values('$data[0]','$data[1]','$data[2]')";
            $result=mysqli_query($con,$sql) or die(mysql_error());
         }
    
         fclose($handle);
         echo "Successfully Imported";
     }
     else
     {
         echo "Invalid File";
     }    
}
?>
</html>
Edited by mac_gyver
code tags around posted code please
Link to comment
Share on other sites

which of your three columns is the only one that is getting a value and is that value the expected value or is it the complete line from the csv file?

 

short-answer: we are not sitting there with you and when you state something like "it only uploads a single column into  my database" we don't know what you saw, but knowing that information tells us where to look at to find the problem.

 

so, it is always helpful to post some example input data you are using with your code, what result you are getting from that input data, what exactly is wrong with that result, and what result you expected to get.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.