Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择然后从数据库中删除_Php_Mysql_Mysqli - Fatal编程技术网

Php 选择然后从数据库中删除

Php 选择然后从数据库中删除,php,mysql,mysqli,Php,Mysql,Mysqli,我正在努力从数据库中删除,但没有加入表,我选择了costum_idprimary键并尝试以这种方式删除,但它似乎只从一个表中删除,而不是从所有表中删除。我尝试在主键之前删除外键,但效果不太好。我必须加入表格然后删除,还是我的方式有效 $select = $mysqli->query("SELECT * FROM costumes WHERE $q"); while ($row = $select->fetch_assoc()) $db_id =

我正在努力从数据库中删除,但没有加入表,我选择了costum_idprimary键并尝试以这种方式删除,但它似乎只从一个表中删除,而不是从所有表中删除。我尝试在主键之前删除外键,但效果不太好。我必须加入表格然后删除,还是我的方式有效

   $select =  $mysqli->query("SELECT * FROM costumes WHERE $q");
   while ($row = $select->fetch_assoc())
            $db_id = $row["costume_id"];

   $costume = $mysqli->query("DELETE FROM costumes WHERE costume_id='$db_id'");
   $costume_row = $costume->fetch_assoc();

   $history = $mysqli->query("DELETE FROM history WHERE history_id='$db_id'");
   $history_row = $history->fetch_assoc();

   $location = $mysqli->query("DELETE FROM location WHERE location_id='$db_id'");
   $location_row = $location->fetch_assoc();

   $state = $mysqli->query("DELETE FROM state WHERE state_id='$db_id'");
   $state_row = $state->fetch_assoc();
$q是一个子流。我的桌子上有:

costumes (
  costume_id varchar(10) primary key,
  name varchar(50), 
  is_seperate bool, 
  is_onepeice bool, 
  description text, 
  color varchar(25), 
  material varchar(50), 
  genre varchar(25), 
  gender char(1), 
  size varchar(5)
);
state (
  state_id varchar(10), 
  in_use bool, 
  fixed bool, 
  condition text, 
  foreign key (state_id) references costume(costume_id)
);
history (
  history_id varchar(10), 
  previous_user varchar(20), 
  profession varchar(20), 
  previous_play varchar(50), 
  date date, fixed_previous bool, 
  comments text, 
  foreign key (history_id) references costume (costume_id)
);
location (
  location_id varchar(10), 
  loc_no varchar(10) primary key, 
  room_no varchar(3), 
  foreign key (location_id) references costumes (costume_id)
);
我的新删除查询是:

 $delete = $mysqli->query("DELETE costumes,history,status,location FROM costumes join   history on costumes.costume_id = history.history_id join status on status.status_id join location on location.location_id where costume_id='$db_id'");

该查询给我带来了约束问题

您应该在delete语句中联接表。因此,请完成此查询,然后重试:

DELETE costumes, history
FROM costumes JOIN history ON costume.costume_id = history.costume_id
WHERE costume_id = '$db_id';

因为我不知道表是如何构造的,所以必须更正联接语法,以匹配通常联接它们的方式。如需进一步帮助,请尝试编辑您的问题,并为我们提供两个表中的所有字段名称。

您应该在delete语句中加入表。因此,请完成此查询,然后重试:

DELETE costumes, history
FROM costumes JOIN history ON costume.costume_id = history.costume_id
WHERE costume_id = '$db_id';
因为我不知道表是如何构造的,所以必须更正联接语法,以匹配通常联接它们的方式。如需进一步帮助,请尝试编辑您的问题,并为我们提供两个表中的所有字段名称。

请尝试以下操作:

DELETE from costumes where costumeid in 
(
Select temp_costume.id from
( Select costume_id as id from table....
) as temp_costume
试试这个:

DELETE from costumes where costumeid in 
(
Select temp_costume.id from
( Select costume_id as id from table....
) as temp_costume

我认为应该使用外键的ON-DELETE-CASCADE属性。使用此选项,当您删除服装时,它也将删除历史记录表中的该客户。

我认为您应该使用外键的ON delete CASCADE属性。使用此选项,当您删除一件服装时,它也会删除历史记录表中的该客户。

andrew answer会更好,Imhoan和Rew answer会更好,imhoI会编辑该帖子。它现在包括我所有的表以及我在文章中编辑的每个表中的内容。它现在包括所有我的表和每个表中的内容您是否有一个现有的SELECT查询,您可以向我们展示它不是基于我们的某个查询。显示您过去如何联接所有表的内容?我没有联接任何表。当我进行选择时,只有一个表您是否有一个现有的选择查询,您可以向我们显示它不是基于我们的查询之一。显示您过去如何联接所有表的内容?我没有联接任何表。当我做选择时,只有一点是好的。如果正确设置外键约束,则从主表中删除行时,其他表中的相关行将被删除:Good point。如果正确设置外键约束,则从主表中删除行时,其他表中的相关行将被删除: