php - Data in CSV file only being imported partially into phpMyAdmin database table -
i'm trying import csv file complete data phpmyadmin database table. however, part of data in csv file being imported. if import csv file @ phpmyadmin, data being imported. there no error show , i'm not sure problem occurs. i'm using load data infile load data database table.
here code:
<?php session_start(); $host = "localhost"; $user = "root"; $password = ""; $db = "smposi"; $con = mysqli_connect($host,$user,$password,$db); $message = ""; $m_sfile = $_session['sfile']; if (isset($_post['submit'])) { $allowed = array('csv'); $filename = $_files['file']['name']; $ext = pathinfo($filename, pathinfo_extension); if (!in_array($ext, $allowed)) { // show error message $message = 'invalid file type, please use .csv file!'; } else { move_uploaded_file($_files["file"]["tmp_name"], "imported csv/" . $_files['file']['name']); $file = "imported csv/" . $_files['file']['name']; $query = <<<eof load data local infile '$file' table $m_sfile fields terminated ',' optionally enclosed '"' lines terminated '\n' ignore 1 lines (sdate2,sdate,location,pcode,code,custarea, pdesc,packet,weight,max,ppacket,lqty,printed, corder,luserid,ltime) eof; $query2 = mysqli_query($con,"delete $m_sfile location=''"); if (!$result = mysqli_query($con, $query)) { exit(mysqli_error($con)); } $message = "csv file imported!"; } } ?>
i have imported csv data below simple script. try out if helps. read complete article import/export csv data.
if(isset($_post["import"])){ $filename=$_files["file"]["tmp_name"]; if($_files["file"]["size"] > 0) { $file = fopen($filename, "r"); while (($getdata = fgetcsv($file, 10000, ",")) !== false) { $sql = "insert employeeinfo (emp_id,firstname,lastname,email,reg_date) values ('".$getdata[0]."','".$getdata[1]."','".$getdata[2]."','".$getdata[3]."','".$getdata[4]."')"; $result = mysqli_query($con, $sql); if(!isset($result)) { echo "<script type=\"text/javascript\"> alert(\"invalid file:please upload csv file.\"); window.location = \"index.php\" </script>"; } else { echo "<script type=\"text/javascript\"> alert(\"csv file has been imported.\"); window.location = \"index.php\" </script>"; } } fclose($file); } } ?>
Comments
Post a Comment