在檢查上傳的CSV文件是否在它們的文件名中包含這些字符後,我試圖從CSV文件中插入值到我的數據庫中。因此,如果文件名有M1在其文件名,我將插入/更新我的數據庫表中的這些領域,具有文件,這樣做有平方米,立方米,並M4對它們的文件名。這裏的代碼:PHP If()..否則如果()然後插入不工作
<?php
$uploadstatus = 1;
$found = 0;
$filename = $file_path;
$file_m1 = "m1";
$file_m2 = "m2";
$file_m3 = "m3";
$file_m4 = "m4";
if (file_exists($filename) && $uploadstatus) {
if (!$found) {
include ('conn.php');
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gen_count
$data8 = mysql_real_escape_string($data[8]); //be_count
$data9 = mysql_real_escape_string($data[9]); //qc_count
$data10 = mysql_real_escape_string($data[10]); //tran_count
$data11 = mysql_real_escape_string($data[11]); //rm_count
$data12 = mysql_real_escape_string($data[12]); //sc_count
$data13 = mysql_real_escape_string($data[13]); //in_count
$data14 = mysql_real_escape_string($data[14]); //en_count
$data15 = mysql_real_escape_string($data[15]); //co_count
$data16 = mysql_real_escape_string($data[16]); //ve_count
$data17 = mysql_real_escape_string($data[17]); //ed_count
// file_m1 = rec_count, gen_count,be_count,qc_count,tran_count
if (strpos($filename , $file_m1)) {
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rec_count,gen_count,be_count,qc_count,tran_count,)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data6','$data7','$data8','$data9','$data10')
ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gen_count=values(gen_count),be_count=values(be_count),
qc_count=values(qc_count),tran_count=values(tran_count)";
}
// file_m2 = rm_count, sc_count, in_count
else if (strpos($filename , $file_m2)) {
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rm_count,sc_count,in_count)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data11','$data12','$data13')
ON DUPLICATE KEY UPDATE rm_count=values(rm_count),sc_count=values(sc_count),in_count=values(in_count)";
}
//file_m3 = en_count, co_count, ve_count, ed_count
else if (strpos($filename , $file_m3)) {
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,en_count,co_count,ve_count,ed_count)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data14','$data15','$data16','$data17')
ON DUPLICATE KEY UPDATE en_count=values(en_count),co_count=values(co_count),ve_count=values(ve_count),ed_count=values(ed_count)";
}
// file_m4 = rm_count, sc_count, in_count,en_count, co_count, ve_count, ed_count
else if (strpos($filename , $file_m4)) {
$query = "INSERT INTO tbltest (rcode,pcode,mcode,bcode,ecode,filetype,rm_count,sc_count,in_count,en_count,co_count,ve_count,ed_count)
VALUES ('$data0','$data1','$data2','$data3','$data4','$data5','$data11','$data12','$data13','$data14','$data15','$data16','$data17')
ON DUPLICATE KEY UPDATE rm_count=values(rm_count),sc_count=values(sc_count),in_count=values(in_count)
en_count=values(en_count),co_count=values(co_count),ve_count=values(ve_count),ed_count=values(ed_count)";
}
mysql_query ($query,$connect);
} //if
} while ($data = fgetcsv($handle,1000,"|")); //do
} //found
} // if exist
} //if server
?>
問題:我可以成功上傳任何文件。問題是,如果文件名有m1或m4,我不能在數據庫表中插入值(來自csv)。當文件帶有平方米和立方米成功地將值(從csv)插入到數據庫表中。當我做echo $query;
結果是正確的值。
Cz this is'if(!$ found){'alwys true。 Cz沒有'$ found'的變量定義 –
我有一個'$ found = 0;'變量聲明 – user3201441
'$ found = 0'並且條件爲'if(!$ found)'總是會是真的,因爲系統會像'if($ found == 0)'這樣讀取它,它與'if($ found == false)'相同。 –