angular 2 upload and read csv file
Import CSV file data into mysql database using php; In this tutorial; you will learn how to how to import CSV file data into MySQL database using PHP script.
Import CSV File Data into MySQL Database using PHP
Follow the below steps to import CSV file data into MySQL using PHP script or lawmaking:
- Step one – Create PHP Project
- Step 2 – Create Table in Database
- Pace 3 – Create a Database Connexion File
- Step four – Create HTML Grade To Upload CSV File
- Step 5 – Create PHP File To Import Csv File Data Into Database
Footstep i – Create PHP Projection
First of all; visit your web server directory and create a php directory; which name demo.
Pace 2 – Create Table in Database
Create table into your database; then visit your PHPMyAdmin and create a table name users with the post-obit fields: name, email, mobile.
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8_unicode_ci Not NULL, `email` varchar(50) COLLATE utf8_unicode_ci Non Zilch, `telephone` varchar(15) COLLATE utf8_unicode_ci Non Cipher, `created_at` datetime Not Zip, `updated_at` datetime NOT NULL, `status` enum('Active','Inactive') COLLATE utf8_unicode_ci NOT Null DEFAULT 'Active', PRIMARY Cardinal (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; Step three – Create a Database Connection File
Create a file proper noun db.php and update the below code into your file.
<?php $servername='localhost'; $username='root'; $password=''; $dbname = "my_db"; $conn=mysqli_connect($servername,$username,$countersign,"$dbname"); if(!$conn){ die('Could not Connect MySql Server:' .mysql_error()); } ?> Pace 4 – Create HTML Grade To Upload CSV File
Create a elementary HTML upload csv file form and add together the post-obit code into your index.php file:
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-eight"> <meta name="viewport" content="width=device-width, initial-scale=i, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.ii/css/bootstrap.min.css"> <championship>Import CSV File into MySQL using PHP</title> <mode> .custom-file-input.selected:lang(en)::after { content: "" !important; } .custom-file { overflow: subconscious; } .custom-file-input { white-space: nowrap; } </way> </caput> <torso> <div grade="container"> <form action="upload.php" method="post" enctype="multipart/form-data"> <div class="input-group"> <div class="custom-file"> <input blazon="file" class="custom-file-input" id="customFileInput" aria-describedby="customFileInput" name="file"> <characterization class="custom-file-characterization" for="customFileInput">Select file</label> </div> <div class="input-grouping-append"> <input type="submit" name="submit" value="Upload" grade="btn btn-primary"> </div> </div> </form> </div> </torso> </html> Stride 5 – Create PHP File To Import Csv File Data Into Database
Create ane file proper name upload.php; which is used to read csv file and insert all csv file data into MySQL database. So add the following code into upload.php file:
<?php // include mysql database configuration file include_once 'db.php'; if (isset($_POST['submit'])) { // Immune mime types $fileMimes = array( 'text/x-comma-separated-values', 'text/comma-separated-values', 'awarding/octet-stream', 'awarding/vnd.ms-excel', 'awarding/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/patently' ); // Validate whether selected file is a CSV file if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['blazon'], $fileMimes)) { // Open uploaded CSV file with read-simply mode $csvFile = fopen($_FILES['file']['tmp_name'], 'r'); // Skip the first line fgetcsv($csvFile); // Parse data from CSV file line past line // Parse data from CSV file line by line while (($getData = fgetcsv($csvFile, 10000, ",")) !== Fake) { // Get row data $name = $getData[0]; $email = $getData[1]; $phone = $getData[2]; $status = $getData[3]; // If user already exists in the database with the same email $query = "SELECT id FROM users WHERE e-mail = '" . $getData[1] . "'"; $check = mysqli_query($conn, $query); if ($check->num_rows > 0) { mysqli_query($conn, "UPDATE users SET name = '" . $name . "', phone = '" . $telephone . "', status = '" . $status . "', created_at = Now() WHERE email = '" . $email . "'"); } else { mysqli_query($conn, "INSERT INTO users (proper name, email, phone, created_at, updated_at, condition) VALUES ('" . $name . "', '" . $email . "', '" . $phone . "', NOW(), At present(), '" . $status . "')"); } } // Close opened CSV file fclose($csvFile); header("Location: alphabetize.php"); } else { echo "Please select valid file"; } } Determination
PHP import data into MySQL database; Through this tutorial, you have learned how to upload csv file and import/insert data into MySQL database using PHP.
Recommended PHP Tutorials
If you have any questions or thoughts to share, use the annotate form below to reach the states.
Source: https://www.tutsmake.com/import-csv-file-into-mysql-using-php/
0 Response to "angular 2 upload and read csv file"
Post a Comment