插入或更新mysql表

时间:2015-02-24 10:51:32

标签: mysql replace insert

Hy All。我有一些代码创建查询,我正在更新数据库。但它删除所有表并重新插入相同的代码。我想要的是更新,如果有必要或插入,如果不存在。

我试过的是这个:

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外键

提前感谢您的暗示

1 个答案:

答案 0 :(得分: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";
    }