[英]how to import csv file to MYSQL in codeigniter which contain html tags
我想使用codeigniter將csv導入mysql。
這是我的源代碼。
我有一個來自客戶端的非標准化事件日志CSV,我試圖將其加載到MySQL表中,以便可以將其重構為合理的格式。
我創建了一個名為“ CSVImport”的表,該表的CSV文件的每一列都有一個字段
function import_questions(){
$question_type=$_POST['question_type'];
$main_category_name=$_POST['main_category_name'];
$sub_category_name=$_POST['sub_category_name'];
$exam_name_list=$_POST['exam_name_list'];
$chapter_name=$_POST['chapter_name'];
$lesson_name=$_POST['lesson_name'];
$difficult_level=$_POST['difficult_level'];
$input_type=$_POST['input_type'];
$config = array();
$config['upload_path'] = './uploads/question_upload/';
$config['allowed_types'] = '*';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$this->load->library('upload',$config);
$tb_name=$_FILES['userfile']['name'];
$tb_type=$_FILES['userfile']['type'];
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('failure', 'File Uploaded Error!.');
header('Location:'.$this->data['base'].'cbulkupload/');
}
else
{
$datas = array('upload_data' => $this->upload->data());
$filename = $datas['upload_data']['full_path'];
require_once './excel/excel_reader2.php';
if($tb_type == 'application/vnd.ms-excel')
{
$data = new Spreadsheet_Excel_Reader($filename);
for($i=0;$i<=count($data->sheets);$i++) // Loop to get all sheets in a file.
{
//echo "count :".count($data->sheets);
//echo "<pre>";print_r($data->sheets[$i][cells]);
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
//echo "<pre>";print_r($data->sheets[$i][cells][$j]);
if($j != 1){
$QUESTION_AREA = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][1]));
$TEXT_OPTION1 = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][2]));
$TEXT_OPTION2 = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][3]));
$TEXT_OPTION3 = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][4]));
$TEXT_OPTION4 = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][5]));
$TEXT_OPTION5 = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][6]));
$RESULT_OPTION = $data->sheets[$i][cells][$j][7];
$ANSWER_DESCRIPTION = mysql_real_escape_string(preg_replace("/\s+/"," ",$data->sheets[$i][cells][$j][8]));
$query = "insert into add_question(MAIN_CATEGORY_ID,SUB_CATEGORY_ID,EXAM_NAME_ID,CHAPTER_ID,LESSON_ID,INPUT_TYPE,DIFFICULT_LEVEL,DIAGNOSIS_VALUE,CREATED_DATE,ACTIVE_STATUS,QUESTION_AREA,TEXT_OPTION1,TEXT_OPTION2,TEXT_OPTION3,TEXT_OPTION4,TEXT_OPTION5,RESULT_OPTION,ANSWER_DESCRIPTION) values(
'".$main_category_name."','".$sub_category_name."','".$exam_name_list."','".$chapter_name."','".$lesson_name."','".$input_type."','".$difficult_level."','".$question_type."',NOW(),'Y','".$QUESTION_AREA."','".$TEXT_OPTION1."','".$TEXT_OPTION2."','".$TEXT_OPTION3."','".$TEXT_OPTION4."','".$TEXT_OPTION5."','".$RESULT_OPTION."','".$ANSWER_DESCRIPTION."')";
mysql_query($query);
}
}
}
}
}
請幫助我獲得更好的解決方案。
上載csv后發現以下錯誤:
502錯誤網關
Once your file is uploaded then u should to fetch a row from uploaded using fgetcsv function of php
My Working Solution:
$p_Filepath ="your file path"
$file = fopen($p_Filepath, 'r');
$data = array();
$i=0;
while (! feof($file)) {
$data[$i] = (fgetcsv($file));
$i++;
}
$data1 = array();
for($j=1;$j<count($data);$j++){
$data1[$j] = array (
"date" => $data[$j][0],
"time" => $data[$j][1],
"type" => $data[$j][2],
"source" => $data[$j][3],
"contactName" => $data[$j][4],
"phone" => $data[$j][5],
"email" => $data[$j][6],
"address" => $data[$j][7],
"city" => $data[$j][8],
"state" => $data[$j][9],
"zip" => $data[$j][10],
"language" => $data[$j][11],
"practiceArea" => $data[$j][12],
"practiceCategory"=> $data[$j][13],
"actionable" => $data[$j][14],
"billable" => $data[$j][15],
"destNum" => $data[$j][16],
"callDuration" => $data[$j][17],
"pageUrl" => $data[$j][18],
"emailRec" => $data[$j][19]
);
$this->db->insert("contactReport",$data1[$j]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.