PHP CSV导入数据库
程序员文章站
2022-04-08 20:12:57
...
duDaoRuConfig.php页面
$csv_key_name){ $result_arr[$i][$csv_key_name] = $data_line[$csv_key_num]; } $i++; } return $result_arr;}?>
doDaoRu.php
doDaoRu2.php
doDaoRu3.php
$table_real_column_name){ if($j == count($table_real_column_name_arr)){ $csv_line_data_value .= " ' ".$result_arr[$i][$csv_real_key_name]."' "; }else{ $csv_line_data_value .= " '".$result_arr[$i][$csv_real_key_name]."', "; } $j++; } $all_insert_data_value_str .= " ($csv_line_data_value) ,";}$all_insert_data_value_str = substr($all_insert_data_value_str,0,-1); //去掉最后一个逗号//拼凑所有需插入值的字段名到一个长字符串的sql语句中$all_insert_column_name_str = "";$i = 1;foreach($table_real_column_name_arr as $csv_real_key_name=>$table_real_column_name){ if($i == count($table_real_column_name_arr)){ $all_insert_column_name_str .= " $table_real_column_name "; }else{ $all_insert_column_name_str .= " $table_real_column_name , "; } $i++;}//问题,1文本中如有回车换行会导致字符串中有转义字符导入失败,2中文乱码//执行批插入,csv导入完成$query = mysql_query("insert into $tableName ($all_insert_column_name_str) values $all_insert_data_value_str");//批量插入数据表中fclose($csv_file);if($query){ echo '导入成功!';}else{ echo '导入失败!';}?>
doDaoRuAjax.php