2015-02-24 206 views
0

Hy全部。我有一些代碼創建查詢,我正在更新數據庫。但它刪除所有表並重新插入相同的代碼。我想要的是更新如果有必要或插入,如果不存在。插入或更新mysql表

我已經試過是這樣的:

INSERT INTO mytable (data, fk_product, fk_entrepot ,value, price, type_mouvement, tip_act, nr_act , id_act, id_linie_act, motiv) VALUES 
('2010-06-14', '64', '1', '1', '4.46', '2', 'BC', '1', '7', '16', 'BC/1/14-06-2010') 
ON DUPLICATE KEY UPDATE 
fk_product = VALUES(fk_product), 
fk_entrepot = VALUES(fk_entrepot); 


REPLACE INTO mytable SET data = '2010-06-14', fk_product = '64', fk_entrepot = '1', value = '1', price = '4.46', type_mouvement = '2', tip_act = 'BC', nr_act = '1', id_act = '7',id_linie_act = '16', motiv = 'BC/1/14-06-2010'; 

ROWID是原發性
fk_product外鍵
fk_entrepot外鍵

預先感謝您的寓意

+0

哪些編程你正在使用的語言 – rohitr 2015-02-24 11:29:02

+0

對不起。我使用的是PHP – 2015-02-24 11:31:26

回答

0
First fire select query with all values you are inserting then check 

then check $num = mysql_num_rows() whether that record exists then check 

if else condition like 

    if($num >0) 
     { 
     echo "fire your update query here"; 
     } 
    else 
    { 
    echo "fire insert query"; 
    }