我有3个表employeeprojects
,projects
和clients
。这是database design。我允许用户从客户端界面删除客户端。但由于所有表都相关,因此我想删除与要删除的projects
相关联的所有employeeprojects
和所有client
。我需要使用客户端名称这样做。是否可以执行JOIN?
标记
<form action="core/query.php" method="POST">
<input type='hidden' name='deleteClientid' id='deleteClientid' value="<?=$client['secret_id'];?>" >
<input type='hidden' name='deleteClientName' id='deleteClientName' value="<?=$client['name'];?>" >
<button data-toggle="tooltip" title="remove Client" type="submit" id="deleteClient" name="deleteClient" class='btn btn-default btn-sm'><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
query.php
//deleteClient
if(isset($_POST['deleteClient'])){
//retrieve value from form
$deleteClientid = $_POST['deleteClientid'];
$deleteClientName = $_POST['deleteClientName'];
//update employeeprojects by deleting the row
$deletefromempprojects= $db->prepare("DELETE $deleteClientName FROM employeeprojects WHERE client_name= '$deleteClientName'");
$deletefromempprojects->bind_param('s', $_POST['deleteClientName']);
$deletefromempprojects->execute()or die(mysqli_error($db));
if(! $deletefromempprojects){
die('Could not update data: ' . mysqli_error($db));
}
//delete from projects table
elseif($deletefromempprojects){
//update projects by deleting the row
$deletefromprojects= $db->prepare("DELETE FROM projects WHERE client_name= '$deleteClientName'");
$deletefromprojects->bind_param('s', $_POST['deleteClientName']);
$deletefromprojects->execute()or die(mysqli_error($db));
}
//delete from clients table
elseif($deletefromclients){
//update clients by deleting the row
$deletefromclients= $db->prepare("DELETE FROM clients WHERE id= '$deleteClientid'");
$deletefromclients->bind_param('s', $_POST['deleteClientid']);
$deletefromclients->execute()or die(mysqli_error($db));
}
$deletefromempprojects->close();
$deletefromprojects->close();
$deletefromclients->close();
$db->close();
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}