PHP PDO - MySQL Query Create or Update (unique index) -


hope well.

i have table in mysql (mariadb) database below schema:

create table `scheduled_immobilise` (   `id` int(11) not null auto_increment,   `account` varchar(32) collate utf8_unicode_ci default null,   `device` varchar(32) collate utf8_unicode_ci default null,   `allow_from` time default null,   `allow_to` time default null,   `active` varchar(6) collate utf8_unicode_ci default null,   `last_updated` timestamp null default null on update current_timestamp,   `cron_id_from` int(11) default null,   `cron_id_to` int(11) default null,   primary key (`id`),   unique key `unique` (`account`,`device`) using btree ) engine=innodb auto_increment=8 default charset=utf8 collate=utf8_unicode_ci; 

what trying write query either create row (if unique index doesn't exist), or if exists update it. know can doing select query first, hoping avoid this. below sql query using create. please note using pdo named placeholders..

insert      scheduled_immobilise (         account,          device,          allow_from,          allow_to,          active     )  values (     :account,      :device,      :allow_from,      :allow_to,     :active ) 

any appreciated, in advance!

paul.

there's mysql option this

insert  scheduled_immobilise (     account,      device,      allow_from,          allow_to,          active     )  values (     :account,      :device,      :allow_from,      :allow_to,     :active ) on duplicate key update account = :account, device = device...etc 

Comments