2015-10-16 86 views
-1

上傳csv文件後,我試圖將其內容插入到我的數據庫表中。我有這個疑問:SQL UPDATE不更新

$connect = mysql_connect("localhost","root",""); 
mysql_select_db("dbtest",$connect); 

//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]); //qc_count 
     $data9 = mysql_real_escape_string($data[9]); //be_count 
     $data10 = mysql_real_escape_string($data[10]); //trn_count 

$query = "INSERT INTO tbltest(rcode,pcode,mcode,bcode,ecode,filetype,rec_count, 
      gen_count,qc_count,be_count,trn_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), 
      qc_count=values(qc_count), be_count=values(be_count), trn_count=values(trn_count)"; 

mysql_query ($query,$connect) ; 
    } 
} while ($data = fgetcsv($handle,1000,"|")); 

,它的工作整齊,但隨後作爲數據庫被重新構建,我就在這時,需要更新數據庫表爲rcode to filetype具有價值已經和我只需要插入的rec_count to trn_count值。所以我的第一個查詢INSERT INTO ... ON DUPLICATE KEY UPDATE已被更改爲UPDATE只。所以我這樣做:

$query = "UPDATE tbltest SET (rec_count='$data6', gen_count = '$data7', 
      qc_count = '$data8', be_count = '$data9', trn_count= '$data10') WHERE 
      (rcode = '$data0', pcode = '$data1', mcode = '$data2', bcode = '$data3', 
      ecode = '$data4', filetype = '$data5')"; 

我現在的問題是,我UPDATE似乎沒有工作,因爲它不更新數據庫表。當我這樣做的時候

$query = "UPDATE tbltest SET rcode = '5'"; 

數據庫正在更新。當我嘗試echo $query;時,回聲響應正確的數據(來自csv)。我只是不知道爲什麼它不會將這些數據插入到數據庫中。請幫助。謝謝

回答

0

您的SQL語法不正確。該聲明應該看起來像

UPDATE tbltest 
SET rec_count='...', gen_count = '...', ... 
WHERE rcode = '...' AND pcode = '...' AND ... 

請參閱MySQL UPDATE syntax

+0

現在這樣做,我會稍後再更新。謝謝 – user3201441

+0

感謝您的回答。 – user3201441