TP5 Importing databases

Source: Internet
Author: User
This article mainly introduces about TP5 import database, has a certain reference value, now share to everyone, the need for friends can refer to

HTML page Code theme:

<form method= "POST" action= "{: url (' Index/savestudentimport ')}" class= "Form-signin" enctype= "Multipart/form-data ">      <p>        <select name=" Themeid ">            {volist name=" theme "id=" V "}                <option value=" {$ V.id} ">{$v .title}</option>            {/volist}        </select>    </p><br/>    <input Name= "Excel" type= "file" class= "Form-control" >      <br/><br/> <button class=    "Btn Btn-lg Btn-primary btn-block "> Import </button>  </form>

Controller Receive code:

Public Function Savestudentimport () {//import (' Phpexcel. Phpexcel ', extend_path);//Method Two vendor ("Phpexcel.phpexcel");            Method One $objPHPExcel = new \phpexcel ();          $themeid =input (' Post.themeid ');//Import data type//Get form upload file $file = Request ()->file (' Excel '); $info = $file->validate ([' Size ' =>1567800, ' ext ' = ' xlsx,xls,csv '])->move (Root_path. ' Public '. Ds.          ' Excel ');  if ($info) {$exclePath = $info->getsavename (); Gets the file name $file _name = Root_path. ' Public '. Ds. ' Excel '. Ds.   $exclePath;              The address of the uploaded file $objReader =\phpexcel_iofactory::createreader (' Excel5 ');  $obj _phpexcel = $objReader->load ($file _name, $encode = ' utf-8 ');              Load file contents, encode utf-8 echo "<pre>";   $excel _array= $obj _phpexcel->getsheet (0)->toarray ();  Convert to array format array_shift ($excel _array);                          Delete the first array (title);              $data = [];       $i = 0;       foreach ($excel _array as $k = + $v) {$data [$k] [' title '] = $v [1];                   $data [$k] [' optiona '] = $v [2];                   $data [$k] [' optionb '] = $v [3];                   $data [$k] [' optionc '] = $v [4];                   $data [$k] [' optiond '] = $v [5];                   $data [$k] [' optione '] = $v [6];                   $data [$k] [' optionf '] = $v [7];                $data [$k] [' time '] = Date (' y-m-d h:i:s ', Time ());                $data [$k] [' themeid '] = $themeid;              $i + +; } $success =db::name (' Sleeptopic ')->insertall ($data);              BULK INSERT data//$i = $error = $i-$success; echo "Total {$i}, success {$success} bar, failed {$error}.             "; Db::name (' t_station ')->insertall ($city);          BULK INSERT data}else{//upload failed get error message echo $file->geterror (); }        }

Related recommendations:

TP5 Project Modification Database

TP5 Phpexcel Generate form instance sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.