0

As explained in the title, I am trying to import data to my table through uploading the csv file to my php page. This is the code I got from EggSlab

<?php
					
					$servername = "localhost";
					$username = "root";
					$password = "";
					$dbname = "Test";

					// Create connection
					$conn = new mysqli($servername, $username, $password, $dbname);
					// Check connection
					if ($conn->connect_error) {
						 die("Connection failed: " . $conn->connect_error);
					}
					
					
?>

<html>

	<body>
		
		<form name="import" method="post" enctype="multipart/form-data">
		<input type="file" name="file" /><br />
        <input type="submit" name="submit" value="Submit" />
		</form>
		
		<?php
if(isset($_POST["submit"]))
{
 $file = $_FILES['file']['tmp_name'];
 $handle = fopen($file, "r");
 $c = 0;
 while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
 {
 $name = $filesop[0];
 $email = $filesop[1];
 
 $sql = "INSERT INTO `xl` (`Name`, `Email`) VALUES ('$name','$email')";

 }
 

 if ($conn->query($sql) === TRUE) {
	echo "You database has imported successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error; 
}
}

?>
	</body>

</html>

The problem is that, when submitting the file, only the last row of my file gets inserted to the table. Any suggestion of a modification to make the code insert every row of the csv file ?

6
  • 1
    execute the query for each iteration in your while loop. Here, you just execute the request at the end, so one time and it's the last row of your csv Commented Nov 7, 2016 at 8:12
  • so, i'm not sure, but try to move your if else statement in the while loop Commented Nov 7, 2016 at 8:14
  • the query is inside the while loop. Commented Nov 7, 2016 at 8:14
  • yes, but the execution of the query is not Commented Nov 7, 2016 at 8:15
  • 1
    Use LOAD DATA INFILE it will be tremendously faster Commented Nov 7, 2016 at 8:45

2 Answers 2

1

This Should fix your problem. You were not executing the query within the loop.

  		<?php
if(isset($_POST["submit"]))
{
 $file = $_FILES['file']['tmp_name'];
 $handle = fopen($file, "r");
 $c = 0;
 while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
 {
  $name = $filesop[0];
  $email = $filesop[1];
 
  $sql = "INSERT INTO `xl` (`Name`, `Email`) VALUES ('$name','$email')";
  if ($conn->query($sql) === TRUE) {
	 echo "You database has imported successfully";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error; 
  }
 }
 

 
}

?>

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

1 Comment

Yes, This worked perfectly. Thank you for your response.
0

CSV file import below See Example:

   <?php 
           $file = $_FILES['file']['tmp_name'];

           //Read the file
            $content = file_get_contents($file);
            //Split file into lines
            $lines = explode("\n", $content);

            //Find columns by reading first line

             for ($i = 1; $i <= 310; $i++) {
                $data = explode("\t", $lines[$i]);
                $name = $filesop[0];
                $email = $filesop[1];

                 $sql = "INSERT INTO `xl` (`Name`, `Email`) VALUES    ('$name','$email')";

        }
    }

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.