我将csv脚本导入mysql时遇到问题:从现在起,我可以将csv的数据放到mysql中,但只能使用严格的标头排列方式,但是我从csv的导出始终会更改表的头,并且我无法控制它。我的csv文件与表格不同,例如:
Name Status Last_name email_address employee_address phone_number
我所需要做的就是获取列名,并将其放入表的正确列中。
谢谢。
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$captabel = true;
$flag = true;
while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {
if($captabel) { $captabel = false; continue; }
$celuleNecesare = array(0);
foreach ($celuleNecesare as $value) {
if(trim($column[$value]) == "" || $column[$value] == NULL)
$flag = false;
}
if($flag) {
$query = "INSERT into test (name ,employee_status, surname, email, address, phone)
values ('" . $column[2] . "','" . $column[7] . "','" . $column[8] . "','" . $column[5] . "','" . $column[0] . "','0" . $column[3] . "')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
header( 'refresh: 0; success.php' );
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
}