php - 'insert into ... on duplicate key' with auto increment -
php - 'insert into ... on duplicate key' with auto increment -
i trying insert if combination of 2 columns (a , b) not exist already. otherwise, want update. issue of next code inserts new row instead of updating when want to. reason think, because don't manage have kind of two-unique-column in settings of table. 1 have solution? google doesn't seem friend today..
the table:
id : int, primary, ai b c , d : intthe code:
$req = $connexion -> prepare(" insert position (a,b,c,d) values (:a,:b,:c,:d) on duplicate key update c=:c;"); $position->bindparam(':a', $a); $position->bindparam(':b', $b); $position->bindparam(':c', $c); $position->bindparam(':d', $d); $a = $val_a; $b = $val_b; $c = $val_c; $d = $val_d; $req -> execute();
on duplicate key requires unique key if not matching primary key. can add together unique key using alter table query
alter table position add together unique key (a,b) php sql on-duplicate-key
Comments
Post a Comment