PHP batch uploads data to the database (.csvformat example, php.csv
Tip: Upload data to A. CSV file
Front-end code:
<Form name = "importForm" action = "import. php "method =" POST "enctype =" multipart/form-data "> <input type =" hidden "value =" import_goods "name =" file "> <table cellpadding =" 2 "cellspacing =" 1 "class =" tb "> <tbody> <tr> <td width =" 200 "> select batch upload document: </td> <input type = "file" name = "upfilename" id = "upfilename" value = ""> </td> </tr> <tr> <td colspan = "2"> <input type = "submit" name = "submit" value = "submit" class = "btn"> </td> </tr> </tbody> </table> </form>
Backend code: import. php file(Only the main code is pasted here. The specific implementation method must be completed by yourself)
If (isset ($ _ POST ['submit ']) {stripos (PHP_ OS, "WIN ")! = False? Setlocale (LC_ALL, ''): setlocale (LC_ALL, 'zh _ CN. GBK '); $ fext = substr ($ _ FILES ['upfilename'] ['name'], strrpos ($ _ FILES ['upfilename'] ['name'], '. ') + 1); if ($ fext! = 'Csv') {die ('upload a file in csv format ', HTTP_REFERER );} $ handle = @ fopen ($ _ FILES ['upfilename'] ['tmp _ name'], "rb"); $ I = 0; $ import_type = ''; if ($ handle) {while ($ line_data = fgetcsv ($ handle, 4096, ',') {if ($ I = 0) {$ import_type = trim ($ line_data [0]); $ I = 1;} elseif (intval ($ line_data [0]) {$ line_list [] = $ line_data ;}}// convert the data format in a loop ($ line_list as $ I =>$ v) {foreach ($ v as $ j =>$ value) {$ l Ine_list [$ I] [$ j] = iconv ('gbk', 'utf-8 // IGNORE ', $ line_list [$ I] [$ j]);} // encode and convert fclose ($ handle); if (! Empty ($ line_list) {// registration number $ sn = array (); $ top_catid = 0; $ name = ''; $ func_name =''; switch ($ import_type) {case 'pattern'; $ top_catid = 5; $ name = 'patent Application No. '; $ func_name = 'dest_import_patent'; break; case 'trademark'; $ top_catid = 4; $ name = 'trademark Registration No. '; $ func_name = 'dest_import_trademark'; break; case 'copyright'; $ top_catid = 2185; $ name = 'registration No '; $ func_name = 'dest_import_copyright'; break; defaul T: die ('the upload document does not specify the production type explicitly! '); Break;} if ($ import_type = 'pattern') {foreach ($ line_list as $ lkey => $ lval) {$ lval [2] = trim ($ lval [2]); if ($ lval [2] = 'patented techno') {// technical patent if (! Empty ($ lval [1]) {if (in_array ($ lval [1], $ sn) {die ('sequence number in the list is '. $ lval [0]. 'production-informed '. $ name. $ lval [1]. 'is the same as the previous one! '); // Determine whether duplicate data exists (based on your needs)} $ sn [] = trim ($ lval [1]);} else {die ('sequence number in the list is '. $ lval [0]. 'production-informed '. $ name. 'Is Blank! ') ;}} Else {// nontechnical patent $ line_list [$ lkey] [1] = '';}} elseif ($ import_type = 'trademark ') {foreach ($ line_list as $ lkey => $ lval) {if (! Empty ($ lval [1]) {if (in_array ($ lval [1], $ sn) {die ('sequence number in the list is '. $ lval [0]. 'production-informed '. $ name. 'is the same as the previous one! ') ;}$ Sn [] = $ lval [1];} else {die ('sequence number in the list is '. $ lval [0]. 'production-informed '. $ name. 'Is Blank! ') ;}} If (! Empty ($ sn) {$ SQL = 'select serial_number from '. $ table_name. 'Where top_catid = '. $ top_catid. AND serial_number IN (\''. implode ("','", $ sn ). '\') '; $ result = $ goods_db-> query ($ SQL); $ r = $ goods_db-> fetch_array (); $ exist = array (); foreach ($ r as $ k =>$ v) {$ exist [] = $ v ['serial _ number'];} if (! Empty ($ exist) die ($ name. 'is :'. implode (',', $ exist ). 'production already exists'); // check whether the same data exists in the Database (based on your needs)} // call the processing function self: $ func_name ($ line_list ); // at this time, $ line_list is the data for you to upload the document, in the array format. Import the data to the database die according to your needs ('batch import is complete! ');}}
. Csv file format:
The case of uploading data to the Data Warehouse (.csv format) with the PHP example is all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support for the customer's house.