我需要将数据插入到表中没有退出的facilities_details表中,下面的代码无法正常运行。
$facility = $_POST["facilities_cb"];
foreach ($facility as $feature)
{
mysqli_query($conn,"INSERT INTO facilities_details(facilitiestype_id,hotel_id) values ('$feature','$hid') WHERE NOT EXISTS (SELECT facilitiestype_id FROM facilities_details)");
}
答案 0 :(得分:1)
没有此类语法WHERE
无法与INSERT
一起使用。如果您想在数据存在时忽略INSERT
,则可以使用INSERT IGNORE
INSERT IGNORE INTO facilities_details(facilitiestype_id,hotel_id)
VALUES ('$feature','$hid')
答案 1 :(得分:0)
试试这样。
IF NOT EXISTS
(SELECT facilitiestype_id
FROM facilities_details)
INSERT INTO facilities_details(facilitiestype_id,hotel_id)
VALUES ('$feature',
'$hid');
答案 2 :(得分:0)
试试这个
insert into facilities_details (facilitiestype_id, hotel_id)
select $feature, $hid where not exists (
select 1 from facilities_details where facilitiestype_id = $feature and hotel_id = $hid
)
或者
insert into facilities_details (facilitiestype_id, hotel_id)
select $feature, $hid where not exists (
select 1 from facilities_details where facilitiestype_id = $feature
)