I am experimenting with bulk import from csv file for my plugin. As I am new to plugin develpoment, please guide me if I am doing something wrong.
What I have:
I have 4 columns of multiple rows in a CSV file. Upon Importing on the admin backend on my plugin, I want 'All' those rows data to be inserted into rows of that particular table in mysql database.
What is the problem:
Only the 1st row is getting inserted. The rest are being ignored.
Here is the php code of my plugin:
<?php
if($_FILES['csv_data']['name']){
$arrFileName = explode('.',$_FILES['csv_data']['name']);
if($arrFileName[1] == 'csv'){
$handle = fopen($_FILES['csv_data']['tmp_name'], "r");
while (($data = fgetcsv($handle, 100, ",")) !== FALSE) {
$chkInId = $data[0];
$chkOutId = $data[1];
$chkInIsbn = $data[2];
$booksReturn = $data[3];
$wpdb->insert($tablename, array(
'bf_chkin_id' =>$chkInId,
'bf_chkout_id' =>$chkOutId,
'bf_chkin_isbn' =>$chkInIsbn,
'bf_chkin_bookreturn' => $booksReturn
));
echo "Inserted";
}
fclose($handle);
print "Import done";
}
}
?>
<!-- Form -->
<form method='post' action='<?= $_SERVER['REQUEST_URI']; ?>' enctype='multipart/form-data'>
<input type="file" name="csv_data" >
<input type='submit' name='submit' value='Upload File'>
</form>
I cannot find out what is the error. Please guide or educate me with a simple example if possible. Thank you in advance.