gpt4 book ai didi

php - "Cannot add or update a child row: a foreign key constraint fails"的问题

转载 作者:行者123 更新时间:2023-11-29 14:54:16 26 4
gpt4 key购买 nike

我似乎可以修复这个小错误。我有一个动态列表菜单,其中包含俱乐部场所的场所 ID。用户可以在我的网站上添加事件,其中包含以下信息:event_id、event_name、event_venue、event_date、estination_id...等。字段Estamentation_id可以为空,默认为空。

上面的设施_id是表设施(设施_id)的外键。

如果用户要添加的事件发生在某个场所,则用户可以选择选择该场所;如果没有与其连接的场所,则可以将其留空。

例如

    Select
Crooked Q'z
The Haven
Brew Bistro

我的动态列表菜单代码

<label for="establishment_link"></label>
<select name="establishment_link" id="establishment_link">
<option selected value="" <?php if (!(strcmp("", $row_establishment_list['establishment_id']))) {echo "selected=\"selected\"";} ?>>Select</option>
<?php
do {
?>
<option value="<?php echo $row_establishment_list['establishment_id']?>"<?php if (!(strcmp($row_establishment_list['establishment_id'],
$row_establishment_list['establishment_name']))) {echo "selected=\"selected\"";} ?>><?php echo $row_establishment_list['establishment_name']?></option>
<?php
} while ($row_establishment_list = mysql_fetch_assoc($establishment_list));
$rows = mysql_num_rows($establishment_list);
if($rows > 0) {
mysql_data_seek($establishment_list, 0);
$row_establishment_list = mysql_fetch_assoc($establishment_list);
}
?>
</select>

但是,当我将其留空时,我收到错误:

Cannot add or update a child row: a foreign key constraint fails (`nnl`.`event`, CONSTRAINT `event_ibfk_1` FOREIGN KEY (`establishment_id`) REFERENCES `establishment` (`establishment_id`))

插入语句:

...

$establishment_link= $_POST['establishment_link'];  
$establishment_link= mysql_real_escape_string($establishment_link);

...

$query2 = "INSERT INTO event(user_id, 
event_name,
event_description,
event_photo_url,
event_thumb_url,
event_link_url,
event_venue,
event_telephone,
event_email,
establishment_id,
event_date,
event_time,
event_entrance_fee,
event_guest_appearances,
event_dress_code,
event_other_details)
VALUES
('$user_id',
'$event_name',
'$event_description',
'$img_large',
'$img_thumb',
'$event_url',
'$event_venue',
'$event_telephone',
'$event_email',
'$establishment_link',
'$date',
'$time',
'$event_fee',
'$event_guests',
'$event_dress',
'$event_other')";
//Execute query
$qry_result2 = mysql_query($query2) or die(mysql_error());

如何解决这个问题?

最佳答案

向我们展示实际的 sql 插入语句。但我的第一个猜测是这样的:

insert into events(...., establishment_id) values(...., '');

虽然它应该设置为NULL

insert into events(...., establishment_id) values(...., NULL);

或者不在列列表中提及estination_id

insert into events(...) values(...);

[问题编辑后]

大概就是我说的吧。尝试将您的查询更改为:

$query2 = "INSERT INTO event(user_id, 
event_name,
event_description,
event_photo_url,
event_thumb_url,
event_link_url,
event_venue,
event_telephone,
event_email,
establishment_id,
event_date,
event_time,
event_entrance_fee,
event_guest_appearances,
event_dress_code,
event_other_details)
VALUES
('$user_id',
'$event_name',
'$event_description',
'$img_large',
'$img_thumb',
'$event_url',
'$event_venue',
'$event_telephone',
'$event_email',
".(empty($establishment_link) ? "NULL" : "'$establishment_link'").",
'$date',
'$time',
'$event_fee',
'$event_guests',
'$event_dress',
'$event_other')";

关于php - "Cannot add or update a child row: a foreign key constraint fails"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5039112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com