LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Import CSV Files Using PHP To MySQL?

How To Import CSV Files Using PHP To MySQL?

How-To-Import-CSV-Files-Using-PHP-To-MySQL
After knowing How To Export MySQL Database Tables To CSV Format Using PHP? its time to know about How To Import CSV Files Using PHP To MySQL?. The import and export of data to and from databases are a common enough procedure in PHP development. Another important activity is the backup and transfer of databases.

PHP is widely used for building a wide range of products ranging from web apps to enterprise-level applications. The key to efficient PHP code is to follow proper workflows and automate processes. The result is high quality and bug-free code.

In almost all PHP applications, data is stored, accessed, and exchanged between various components of the app. To make sure that this exchange and access to data goes smoothly and without any issues, the development team must make sure that the databases and data dumps are in a proper format.

How To Import CSV Files Using PHP To MySQL?

A step-by-step guide to importing data from CSV to MySQL using PHP. It is a basic task for any application that needs a reporting feature to CSV; here we are going to explain how to fill a SQL table file from the CSV file programmatically using PHP.

How-To-Import-CSV-Files-Using-PHP-To-MySQL

HTML:

/****************************************************************************/
// Front End HTML Form To Import CSV Files Using PHP To MySQL
/****************************************************************************/
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file"/>
    <button type="submit" name="Import">Import</button>
</form>

PHP:

/****************************************************************************/
// Import CSV Files Using PHP To MySQL
/****************************************************************************/
<?php
if(isset($_POST["Import"])){	
    $filename=$_FILES["file"]["tmp_name"];		
        if($_FILES["file"]["size"] > 0){
            $file = fopen($filename, "r");
	        while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) {
	            $sql = "INSERT into employeeinfo (emp_id,firstname,lastname,email,reg_date) 
                    values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."')";
                    $result = mysqli_query($con, $sql);
		    if(!isset($result)) {
		        echo "<script type=\"text/javascript\">
			alert(\"Invalid File:Please Upload CSV File.\");
			window.location = \"index.php\"
			</script>";		
		    } else {
			echo "<script type=\"text/javascript\">
			alert(\"CSV File has been successfully Imported.\");
			window.location = \"index.php\"
			</script>";
		    }
	        }	
	        fclose($file);	
	}
}	 
?>

Explanation:

When the upload button is clicked, the temporary file name will be stored in memory and using the while loop the data is saved in $getData variable. Once the process has been completed, the data is sorted column-wise and then finally inserted in the employeeinfo table.

Recommended For You:
Multiple Images DragDrop Uploader With Preview Using Pure JavaScript

Note that fgetcsv() parses lines from the open file, checking for CSV fields and fopen() opens a file or a URL. This code could be tested by importing a CSV file with test data.

Customization:

You can change anything in the upper code if you are pro else we do not recommend you to edit the code as it may break while executing and can cause fatal errors to your server.

Troubleshooting the Errors

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your file then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problem please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
Simple PHP Web Page Counter Without Database

You Like It, Please Share This Recipe With Your Friends Using...

6 Responses to “How To Import CSV Files Using PHP To MySQL?”

  1. SketchUp Pro Crack Free Download says:

    Really i appreciate the effort you made to share the knowledge.The topic here i found was really effective to the topic which i was researching for a long time.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks for reading this article.

  2. Lumion 10 Pro crack says:

    Really i appreciate the effort you made to share the knowledge.The topic here i found was really effective to the topic which i was researching for a long time.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks for reading this article.

  3. Trillian Pro says:

    Interesting! Thanks for sharing! I always Loves your blogs.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks for reading this article.

Leave a Reply

Your email address will not be published. Required fields are marked *