使用 php 将 excel/csv 文件导入 phpmyadmin
Import excel/ csv file into phpmyadmin using php
我正在尝试编写一个代码,使用 PHP 将文档直接上传到 phpMyAdmin。我尝试了这段代码,看起来它可以工作并且没有错误,但是数据没有上传到数据库中。
有人可以帮忙指出问题吗?
<?php
$host="localhost";
$username = "root";
$password = "";
$database= "schoolydb";
$connect = new mysqli($host,$username,$password,$database);
$connect ->set_charset("utf8");
$message = '';
if(isset($_POST["upload"]))
{
if($_FILES['product_file']['name'])
{
$filename = explode(".", $_FILES['product_file']['name']);
if(end($filename) == "csv")
{
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
while($data = fgetcsv($handle))
{
$student_id = mysqli_real_escape_string($connect, $data[0]);
$student_login = mysqli_real_escape_string($connect, $data[1]);
$student_password = mysqli_real_escape_string($connect, $data[2]);
$student_first_name = mysqli_real_escape_string($connect, $data[3]);
$student_last_name = mysqli_real_escape_string($connect, $data[4]);
$student_phone_number = mysqli_real_escape_string($connect, $data[5]);
$student_gender = mysqli_real_escape_string($connect, $data[6]);
$original_back_school = mysqli_real_escape_string($connect, $data[7]);
$original_end_time = mysqli_real_escape_string($connect, $data[8]);
$original_class = mysqli_real_escape_string($connect, $data[9]);
$class_Halom= mysqli_real_escape_string($connect, $data[10]);
$parent_id = mysqli_real_escape_string($connect, $data[11]);
$teacher_id = mysqli_real_escape_string($connect, $data[12]);
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ($student_id, '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
mysqli_query($connect, $query);
}
fclose($handle);
header("location: index.php?updation=1");
}
else
{
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
}
else
{
$message = '<label class="text-danger">Please Select File</label>';
}
}
if(isset($_GET["updation"]))
{
$message = '<label class="text-success">Updation Done</label>';
}
$query = "SELECT * FROM student";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Mysql Database through Upload CSV File using PHP</title>
<script src="../jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="../bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">Update Mysql Database through Upload CSV File using PHP</h2>
<br />
<form method="post" enctype='multipart/form-data'>
<p><label>Please Select File(Only CSV Formate)</label>
<input type="file" name="product_file" /></p>
<br />
<input type="submit" name="upload" class="btn btn-info" value="Upload" />
</form>
<br />
<?php echo $message; ?>
<h3 align="center">Student table</h3>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>student_id</th>
<th>student_login</th>
<th>student_password</th>
<th>student_first_name</th>
<th>student_last_name</th>
<th>student_phone_number</th>
<th>student_gender</th>
<th>original_back_school</th>
<th>original_end_time</th>
<th>original_class</th>
<th>class_Halom</th>
<th>parent_id</th>
<th>teacher_id</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["student_id"].'</td>
<td>'.$row["student_login"].'</td>
<td>'.$row["student_password"].'</td>
<td>'.$row["student_first_name"].'</td>
<td>'.$row["student_last_name"].'</td>
<td>'.$row["student_phone_number"].'</td>
<td>'.$row["student_gender"].'</td>
<td>'.$row["original_back_school"].'</td>
<td>'.$row["original_end_time"].'</td>
<td>'.$row["original_class"].'</td>
<td>'.$row["student_login"].'</td>
<td>'.$row["class_Halom"].'</td>
<td>'.$row["parent_id"].'</td>
<td>'.$row["teacher_id"].'</td>
</tr>
';
}
?>
</table>
</div>
</div>
</body>
</html>
and this is the student table from my database
.......
行中有错误
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ($student_id, '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
$student_id
没有写在' '
内
应该是 '$student_id'
所以实际代码将是
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ('$student_id', '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
这将解决您的问题。
由于您使用的是 .csv 文件,因此您可以告诉 PHP 您的分隔符是分号。
<?php
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
?>
要搜索文件的每个值,您可以使用如下策略:
<?php
$row = 0;
if (($handle = fopen("c:\temp\test.csv", "r")) !== FALSE)
{
//Searching line by line of the file
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$num = count($data);
$row++;
//Searching column by column of the current line
for ($col = 0; $col < $num; $col++)
{
//Here you can use the desired values
switch ($col) {
case 0:
$student_id = mysqli_real_escape_string($connect,$data[$col]);
break;
case 1:
$student_login = mysqli_real_escape_string($connect,$data[$col]);
break;
case 2:
$student_password = mysqli_real_escape_string($connect,$data[$col]);
break;
case 3:
$student_first_name = mysqli_real_escape_string($connect,$data[$col]);
break;
case 4:
$student_last_name = mysqli_real_escape_string($connect,$data[$col]);
break;
case 5:
$student_phone_number = mysqli_real_escape_string($connect,$data[$col]);
break;
case 6:
$student_gender = mysqli_real_escape_string($connect,$data[$col]);
break;
case 7:
$original_back_school = mysqli_real_escape_string($connect,$data[$col]);
break;
case 8:
$original_end_time = mysqli_real_escape_string($connect,$data[$col]);
break;
case 9:
$original_class = mysqli_real_escape_string($connect,$data[$col]);
break;
case 10:
$class_Halom = mysqli_real_escape_string($connect,$data[$col]);
break;
case 11:
$parent_id = mysqli_real_escape_string($connect,$data[$col]);
break;
case 12:
$teacher_id = mysqli_real_escape_string($connect,$data[$col]);
break;
}//end switch
}//endfor (columns of each line)
//If your file has a header and you wish to skip it, you can do something like this:
//if ($row > 1) { //create your query and execute it... }
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`,`teacher_id`) VALUES ";
$query.= "($student_id, '$student_login','$student_password','$student_first_name','$student_last_name','$student_phone_number','$student_gender','$original_back_school','$original_end_time','$original_class','$class_Halom', $parent_id, $teacher_id) ";
mysqli_query($connect, $query);
//}
}
fclose($handle);
}
?>
我正在尝试编写一个代码,使用 PHP 将文档直接上传到 phpMyAdmin。我尝试了这段代码,看起来它可以工作并且没有错误,但是数据没有上传到数据库中。 有人可以帮忙指出问题吗?
<?php
$host="localhost";
$username = "root";
$password = "";
$database= "schoolydb";
$connect = new mysqli($host,$username,$password,$database);
$connect ->set_charset("utf8");
$message = '';
if(isset($_POST["upload"]))
{
if($_FILES['product_file']['name'])
{
$filename = explode(".", $_FILES['product_file']['name']);
if(end($filename) == "csv")
{
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
while($data = fgetcsv($handle))
{
$student_id = mysqli_real_escape_string($connect, $data[0]);
$student_login = mysqli_real_escape_string($connect, $data[1]);
$student_password = mysqli_real_escape_string($connect, $data[2]);
$student_first_name = mysqli_real_escape_string($connect, $data[3]);
$student_last_name = mysqli_real_escape_string($connect, $data[4]);
$student_phone_number = mysqli_real_escape_string($connect, $data[5]);
$student_gender = mysqli_real_escape_string($connect, $data[6]);
$original_back_school = mysqli_real_escape_string($connect, $data[7]);
$original_end_time = mysqli_real_escape_string($connect, $data[8]);
$original_class = mysqli_real_escape_string($connect, $data[9]);
$class_Halom= mysqli_real_escape_string($connect, $data[10]);
$parent_id = mysqli_real_escape_string($connect, $data[11]);
$teacher_id = mysqli_real_escape_string($connect, $data[12]);
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ($student_id, '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
mysqli_query($connect, $query);
}
fclose($handle);
header("location: index.php?updation=1");
}
else
{
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
}
else
{
$message = '<label class="text-danger">Please Select File</label>';
}
}
if(isset($_GET["updation"]))
{
$message = '<label class="text-success">Updation Done</label>';
}
$query = "SELECT * FROM student";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Mysql Database through Upload CSV File using PHP</title>
<script src="../jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="../bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">Update Mysql Database through Upload CSV File using PHP</h2>
<br />
<form method="post" enctype='multipart/form-data'>
<p><label>Please Select File(Only CSV Formate)</label>
<input type="file" name="product_file" /></p>
<br />
<input type="submit" name="upload" class="btn btn-info" value="Upload" />
</form>
<br />
<?php echo $message; ?>
<h3 align="center">Student table</h3>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>student_id</th>
<th>student_login</th>
<th>student_password</th>
<th>student_first_name</th>
<th>student_last_name</th>
<th>student_phone_number</th>
<th>student_gender</th>
<th>original_back_school</th>
<th>original_end_time</th>
<th>original_class</th>
<th>class_Halom</th>
<th>parent_id</th>
<th>teacher_id</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["student_id"].'</td>
<td>'.$row["student_login"].'</td>
<td>'.$row["student_password"].'</td>
<td>'.$row["student_first_name"].'</td>
<td>'.$row["student_last_name"].'</td>
<td>'.$row["student_phone_number"].'</td>
<td>'.$row["student_gender"].'</td>
<td>'.$row["original_back_school"].'</td>
<td>'.$row["original_end_time"].'</td>
<td>'.$row["original_class"].'</td>
<td>'.$row["student_login"].'</td>
<td>'.$row["class_Halom"].'</td>
<td>'.$row["parent_id"].'</td>
<td>'.$row["teacher_id"].'</td>
</tr>
';
}
?>
</table>
</div>
</div>
</body>
</html>
and this is the student table from my database
.......
行中有错误
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ($student_id, '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
$student_id
没有写在' '
内
应该是 '$student_id'
所以实际代码将是
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`, `teacher_id`) VALUES ('$student_id', '$student_login','$student_password','$student_first_name','$student_last_name', '$student_phone_number','$student_gender','$original_back_school',' $original_end_time','$original_class','$class_Halom','$parent_id','$teacher_id') ";
这将解决您的问题。
由于您使用的是 .csv 文件,因此您可以告诉 PHP 您的分隔符是分号。
<?php
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
?>
要搜索文件的每个值,您可以使用如下策略:
<?php
$row = 0;
if (($handle = fopen("c:\temp\test.csv", "r")) !== FALSE)
{
//Searching line by line of the file
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$num = count($data);
$row++;
//Searching column by column of the current line
for ($col = 0; $col < $num; $col++)
{
//Here you can use the desired values
switch ($col) {
case 0:
$student_id = mysqli_real_escape_string($connect,$data[$col]);
break;
case 1:
$student_login = mysqli_real_escape_string($connect,$data[$col]);
break;
case 2:
$student_password = mysqli_real_escape_string($connect,$data[$col]);
break;
case 3:
$student_first_name = mysqli_real_escape_string($connect,$data[$col]);
break;
case 4:
$student_last_name = mysqli_real_escape_string($connect,$data[$col]);
break;
case 5:
$student_phone_number = mysqli_real_escape_string($connect,$data[$col]);
break;
case 6:
$student_gender = mysqli_real_escape_string($connect,$data[$col]);
break;
case 7:
$original_back_school = mysqli_real_escape_string($connect,$data[$col]);
break;
case 8:
$original_end_time = mysqli_real_escape_string($connect,$data[$col]);
break;
case 9:
$original_class = mysqli_real_escape_string($connect,$data[$col]);
break;
case 10:
$class_Halom = mysqli_real_escape_string($connect,$data[$col]);
break;
case 11:
$parent_id = mysqli_real_escape_string($connect,$data[$col]);
break;
case 12:
$teacher_id = mysqli_real_escape_string($connect,$data[$col]);
break;
}//end switch
}//endfor (columns of each line)
//If your file has a header and you wish to skip it, you can do something like this:
//if ($row > 1) { //create your query and execute it... }
$query = "INSERT INTO `student`(`student_id`, `student_login`, `student_password`, `student_first_name`, `student_last_name`, `student_phone_number`, `student_gender`, `original_back_school`, `original_end_time`, `original_class`, `class_Halom`, `parent_id`,`teacher_id`) VALUES ";
$query.= "($student_id, '$student_login','$student_password','$student_first_name','$student_last_name','$student_phone_number','$student_gender','$original_back_school','$original_end_time','$original_class','$class_Halom', $parent_id, $teacher_id) ";
mysqli_query($connect, $query);
//}
}
fclose($handle);
}
?>