php - Insert CSV file into mysql database -


i want insert csv file in database using php. tried following code gives following error

fatal error: call member function exec() on null in c:\xampp\htdocs\ict_project\class.import.php on line 25

<?php include 'connection.php';  class import { private $pdo;  public function __construct() {     $obj_connect = new dbconnect();     $this->pdo = $obj_connect->db_con;  }  public function import_csv() {      $extension= end(explode(".", basename($_files['file']['name'])));     if (isset($_files['file']) && $_files['file']['size'] < 10485760 && $extension=='csv') {         $file = $_files['file']['tmp_name'];          $handle = fopen($file, "r");          try {                        while (($data = fgetcsv($handle, 1000, ",")) !== false) {                 //$num = count($data);              $importsql = "insert tbl_applicants(application_no, applicant_name,applicant_email, applicant_mobile, applicant_address) values('$data[0]','$data[1]')";              $this->pdo->exec($importsql);               }         }         catch(pdoexception $e) {             echo $e->getmessage();         }      } } 

my tbl_applicants have 5 fields named: application_no, applicant_name, applicant_email, applicant_mobile, applicant_address. in csv file have tow row of data above 5 fields. not understand insert query?

hi, here updated code works fine. now want skip first row of .csv file contains header(id,name, email, address). how can this?

<?php include 'connection.php';  class import {     private $pdo;      public function __construct() {              $obj_connection = new db_connection();         $this->pdo = $obj_connection->connection();          }      public function import_csv() {          $extension= end(explode(".", basename($_files['file']['name'])));         if (isset($_files['file']) && $_files['file']['size'] < 10485760 && $extension=='csv') {             $file = $_files['file']['tmp_name'];              $handle = fopen($file, "r");              try {                            while (($data = fgetcsv($handle, 1000, ",")) !== false) {                                    $importsql = "insert tbl_applicants ( application_no, applicant_name, applicant_email, applicant_mobile, applicant_address ) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";                 $this->pdo->query($importsql);                   }             }             catch(pdoexception $e) {                 echo $e->getmessage();             }          }     }  } 

having not seen data guess beased on have used above perhaps need add remaining fields csv?

$importsql = "insert `tbl_applicants`    ( application_no, applicant_name, applicant_email, applicant_mobile, applicant_address )     values    ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')" 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -