New to php and don’t know how to upload and save CSV file in mysql database? Don’t worry in this post I am going to discuss with you how to do that.
Before I jump into the code let’s see what we are going to do.
Steps: How to upload CSV and save in Mysql database using PHP
- Create a html form with input file field.
- Add some CSS Style
- Create connection to database.
- Create table in which we will save CSV data.
HTML Code
When we want to add file field in the form then we must add enctype=”multipart/form-data”. Enctype attribute identifies how the form data should be encoded when data is going to server and it can be used only if the form method is POST. $_GET[‘file’] will use to print success message and $errorMsg will print error message if file will not a CSV.
CSS
Mysql Table Structure
I am using some php built-in function to accomplish CSV Upload task here they are:
- Pathinfo (path,options): returns path information in array form. It takes 2 parameters. First is file path which is required and second is options parameter which is optional.
- move_uploaded_file(file,newloc): moves uploaded file into new location. It also takes 2 required parameters. First parameter specifies the file to be moved and second specifies the new location of the current uploaded file.
- fopen(filename,mode,include_path,context): opens a file or URL. If file or URL not exist it returns FALSE. Fopen() function takes 4 parameter filename and mode are required parameter, include_path and context are optional parameter. If you want to discover more about fopen then visit php.net fopen().
- fgetcsv(file,length,separator,enclosure): parses a line from an open file and returns array of each row in CSV file. fgetcsv() takes 4 paramter. First parameter is required and rests of three are optional.
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?php
/*database connection*/
$host = "localhost";
$user = "root";
$pass = "";
$db = "my_website";
$conn = mysqli_connect($host, $user, $pass,$db);
if(!$conn)
{
die("Error: ".mysqli_connect_error());
}
/*end database connection*/
//if form is submit
if(isset($_POST['submitFile']))
{
$uploadDirectory = "uploads/";
$createUploadPath = $uploadDirectory.$_FILES['uploadCsv']['name'];
$csvFileType = pathinfo($createUploadPath, PATHINFO_EXTENSION);
if ($csvFileType == 'csv')
{
if (move_uploaded_file($_FILES['uploadCsv']['tmp_name'], $createUploadPath))
{
$records = fopen($createUploadPath, 'r');
$rowCounter = 0;
while (($data = fgetcsv($records)) !== FALSE)
{
if($rowCounter > 0)
{
$studentName = $data[0];
$studentMarks = $data[1];
$studentGrade = $data[2];
$qry = "insert into students_grade (student_name, student_grade, student_marks) values('".$studentName."', '".$studentGrade."', '".$studentMarks."')";
$rs = mysqli_query($conn,$qry);
}
$rowCounter++;
}
header("location:index.php?file=success");
exit();
}
}
else
{
$errorMsg = "Please upload Csv file";
}
}
?>
|
0 comments:
Post a Comment