php - How to perform MySQL UPSERT -


this how table looks like.

create table `answers` ( `id` int(11) not null auto_increment, `basicinfo_id` int(11) not null, `question_id` int(11) not null, `answer` int(11) not null, primary key (`id`), index `question_id` (`question_id`), index `basicinfo_id` (`basicinfo_id`), constraint `basicinfo_id` foreign key (`basicinfo_id`) references `basic_info` (`id`) on update no action on delete no action, constraint `question_id` foreign key (`question_id`) references `questions` (`id`) on update no action on delete no action) collate='latin1_swedish_ci' engine=innodb auto_increment=3383; 

this how data can

enter image description here

i want update answers if exist "basicinfo_id". or if change answer question_id 1, 2 , 3. how can upsert data.?

i have tried query doesn't update result.

insert answers (basicinfo_id, question_id, answer) values('98', 1, '1'),('98', 2, '1'),('98', 3, '1'),('98', 4, '1'),('98', 5, '1') on duplicate key update basicinfo_id = 98; 

i have defined composite unique key , how looks still didn't work.

create table `answers` ( `id` int(11) not null auto_increment, `basicinfo_id` int(11) not null, `question_id` int(11) not null, `answer` int(11) not null, primary key (`id`), unique index `basicinfo_id_question_id` (`basicinfo_id`, `question_id`), index `question_id` (`question_id`), index `basicinfo_id` (`basicinfo_id`), constraint `basicinfo_id` foreign key (`basicinfo_id`) references `basic_info` (`id`) on update no action on delete no action, constraint `question_id` foreign key (`question_id`) references `questions` (`id`) on update no action on delete no action ) collate='latin1_swedish_ci' engine=innodb auto_increment=3718; 

your on duplicate key update basicinfo_id = 98; malformed

try : on duplicate key update 'answer' = values('answer');

if unique key exists update answer field


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -