dsgwdigu84950 2019-03-10 17:36
浏览 62
已采纳

使用php将excel / csv文件导入phpmyadmin

I'm trying to write a code that will upload a document directly to phpMyAdmin using PHP. I tried this code and It looks like it works and there are no errors, but the data was not uploaded to the database.. Can someone please help point out the problem?

<?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

..............

  • 写回答

2条回答 默认 最新

  • dongzhunnai0140 2019-03-10 18:38
    关注

    There is an error in the line

    $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 is not written within ' ' It should be like '$student_id' So actual code will be

    
    $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') ";
    

    This will solve your issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?