2009-06-12 76 views
0

我從具有18列的文件中插入數據,我想將其插入的表有20列。其中2列已包含數據。如何在不覆蓋現有數據的情況下將文件插入表中。mysql替換表中的列

下面是一個代碼的例子。注意$line .= ',,'正在追加兩列,因爲更新將不起作用,除非這些列匹配20列的精確表大小。但是$line .= ',,'會覆蓋已經插入的數據。我該怎麼辦?

$fcontents = file('data.txt'); 

if(mysql_error()) 
    echo mysql_error(); 

for($i=0; $i<sizeof($fcontents); $i++) 
{ 
    //strip out double quotes 
    $line = ereg_replace('"','',trim($fcontents[$i])); 

    $line .= ',,'; 

    //strip replace semi-colon with blank spaces 
    $line = ereg_replace(';',' ',$line); 

    //single quote in parts records breaks replace code, removing single quote... 
    $line = ereg_replace("'",'',$line); 


    //breaks apart the tab delimited row into a array 
    $arr = explode(",", $line); 

    //add comma deliminted data back 
    mysql_query("REPLACE INTO products VALUES ('". implode("','", $arr) ."')"); 


} 

回答

4

指定的列名:

REPLACE INTO products (col1, col2, ...) VALUES (val1, val2, ...) 

MySQL online manual

+0

我該如何指定implode中的列名? – payling 2009-06-12 20:31:10