Import HHcsv file in MySQL using PHP

Import csv file in MySQL using PHP

Import csv file in MySQL using PHPPP with example :

Let us tellfg you how to Import csv file in MySQL using PHP with example by steps –

smkdsjndk mskldsdsdm sd skiodsd sd,ms

skdjsjdhsjd skmdskdjksd kjiosdnsmdnlksjdns

fdmfndf dfd f343434

sdmjsdsd s343434

Step : 1– First create Database named “studentdb” & table named “student_details“.

Database Table Structure

Step : 2- Create dbconnection.php file add bellow codes for database connection –

<?php 
	// crete database connection
	$hostname = 'localhost';
	$dbUsername = 'root';
	$dbPadfdfdfdf
	if ($con->connect_error) {
		die("Unable to connect database: " . $con->connect_error);
	}
?>

Step : 3- Create importForm.php file and add bellow codes –

<?php 
	include("dbconnection.php"); // database connection
?>
<!DOCTYPE html>
<html>edffrdfdf
		<script typefd
                } else {
                    return true;
                }           
            });
			
			// check file type
			$("#fileUpload").change(function () {
				var validExtensions = ["csv"]
				var file = $(this).val().split('.').pop();
				if (df
					<div class="row the-wrap">
						<div class="form-horizontal bootstrap-validator-form">
							<div class="users form">
								<form action="importProcess.php" method="post" enctype="multipart/form-data" id="importFrm">
								<div class="row justify-content-between">
									<div class="col-sm-4">
										<input type="file" name="file" id="fileUpload" class="file" />
									</div>
									<div class="col-sm-4 submit">
										<input type="submit" class="btn btn-primary" name="importSubmit" id="importSubmit" value="Import"/>
									</div>                                            
								</div>
								</form>
							</div>
						</div>
					</div>                  
					</div>                   
				</div>df
				</thead>
				
				<tbody>
				<?php 
				$srNo=1;
				$dataQrs = mysqli_query($con,"SELECT * FROM student_details");
				while($results = mysqli_fetch_array($dataQrs)){				
				?>dfdffdfdf
						} else {
							echo "Unpaid";
						}
					?></td>									
				</tr>
				<?php } $srNo++;?>
				</tbody>				
				</table>				
				</div>
				</div>
			</div>				
		</div>
	</body>
</html>
  • In this example, js & css file hav dfmjkfjkd fjn fdjmfkdf ,jkdf d,fme been added for form validation & design.
  • Code has been added here for show the records after import the data.
  • dfk,djkfd fm,d fdfmjopdf df3 343434 dlfdf d,.flmj31p4 34.l34mk3o4

Step : 4- Create importProcess.php file and add bellow codes –

<?php
	include("dbconnection.php"); // Database conection file include		
		   
	if (isset($_POST['importSubmit'])) { 
		//validate whether udfd
				
				while (($line = fgetcsv($csvFile)) !== FALSE) {				
					$noRec = CheckCatRegNocheck($line[4], $con);					
					if ($noRec > 0) {								    
						mysqli_query($con,"UPDATE student_details SET name =  '" . $line[0] . "', date_of_birth = '" . $line[1] . "', mobile_no = '" . $line[2] . "', email_id = '" . $line[3] . "' , is_fee_paid = '" . $line[5] . "'WHERE registration_no = '" . $line[4] . "'");
					} else {						
						if (!empty($line[0])) {						
						   mysqli_query($con,"INSERT INTO student_details (name, date_of_birth, mobile_no, email_id, registration_no, is_fee_paid) VALUES ('" . $line[0] . "','" . $line[1]. "','" . $line[2]. "','" . $line[3]. "','" . $line[4]. "','" . $line[5]."')");
						}
					}
				}
				header("Location: importForm.php");				
				fclose($csvFile);				
			}
		}
	}
	
	// check exiting records from registration no.
	function CheckCatRegNocheck($regno, $con) {
		$res_sql = "SELECT * FROM student_details WHERE registration_no = '$regno'";	   
		$results = mysqli_query($con, $res_sql);
		$totalRec = mysqli_num_rows($results);
		return $totalRec;  
    }
?>

Step : 5- Create csv file as bellow format and import it –

  • You can add/remove student data according to your choice.

Source Code Output

  • After import data, form will look like as –
Import csv file in MySQL using PHP

Click here to more …

Import csv file in MySQL using PHP pro

Ramesh gome to the school of css HTML

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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