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>

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.

you last reply is not what you stated at the start of this thread and it would appear your last reply is what your other current thread is about. please don't start multiple threads for the same problem and we can only help you when you clearly state what problem you are having with your code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.