我想使用php在csv文件中获取最后一个插入ID。我想使用php在MS-Access数据库中导入csv。
目前我的代码工作正常,唯一值得关注的是当我第二次尝试上传时,我在最后一次插入id后插入数据。
这是我的csv文件的id列。
这是代码。
if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0){
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
echo $type = $data[31];
$row = 0;
$last_row = array_pop($data[]);
//echo $last_data = str_getcsv($last_row);
//loop through the csv file and insert into database
do {
//$values = implode(",",$data);
if ($row == 0) {//skip first row (headers)
$row++;
} else {
$type = $data[31];
if($type == 'industrial'){
if ($data[0]) {
$sql1_ind = "INSERT INTO colours(COLNO, COLNAME, MAKER, SOURCE, BASE, undercoat, colourcode, application) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."'
)";
if($database_ind->query($sql1_ind)) {
echo 'Data is imported successfully in Industrial Database.';
echo '<br>';
} else {
$db_err = $database_ind->errorInfo();
}
for($i=0;$i<6;$i=$i+1){
$colno = $data[0];
$tint_base_form = $data[4];
$ingred_form = $data[8+3*$i];
$y = $data[9+3*$i];
$unit = $data[10+3*$i];
if($ingred_form!=''){
$qty = $y*2 + $unit;
$seq = 1+$i;
$sql2_ind = "INSERT INTO colour_details(colno, seq, tintcode, qty) VALUES('".$colno."', '".$seq."', '".$tint_base_form."', '".$qty."')";
$result_sql2_ind = $database_ind->query($sql2_ind);
if($database_ind->query($sql2_ind)) {
echo 'Data is inserted successfully in colour details table. ';
echo '<br>';
} else {
$db_err = $database_ind->errorInfo();
}
}
}
}
} else {
if ($data[0]) {
$sql1_ind = "INSERT INTO colours(COLNO, COLNAME, MAKER, SOURCE, BASE, undercoat, colourcode, application) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."'
)";
if($database_deco->query($sql1_ind)) {
echo 'Data is imported successfully in Decorative Database.';
echo '<br>';
} else {
$db_err = $database_deco->errorInfo();
}
for($i=0;$i<6;$i=$i+1){
$colno = $data[0];
$tint_base_form = $data[4];
$ingred_form = $data[8+3*$i];
$y = $data[9+3*$i];
$unit = $data[10+3*$i];
if($ingred_form!=''){
$qty = $y*2 + $unit;
$seq = 1+$i;
$sql2_ind = "INSERT INTO colour_details(colno, seq, tintcode, qty) VALUES('".$colno."', '".$seq."', '".$tint_base_form."', '".$qty."')";
$result_sql2_ind = $database_deco->query($sql2_ind);
if($database_deco->query($sql2_ind)) {
echo 'Data is inserted successfully in colour details table. ';
echo '<br>';
} else {
$db_err = $database_deco->errorInfo();
//echo 'Error : ('. $db_err[2] .')';
//echo 'Error : ('. $db_err[0] .') -- ' . $db_err[2];
//echo 'ERROR';
}
}
}
}
}
}
} while ($data = fgetcsv($handle,1000,",","'"));
请给我一些建议。