[英]How to delete row with PDO and with restriction
我在mysql數據庫中有兩個表,第一個是“ kavomati”,其中是咖啡機,第二個表是“ lokacije”,其中,這些咖啡機的位置,這些表與數據庫中的主鍵有關系,並且設置為ON DELETE RESTRICT所以我不能刪除一些有咖啡機的地方,那很好。 我創建了一個頁面,在該頁面上可以看到所有位置,並在其中有用於添加新位置的模式按鈕,用於編輯每個位置的模式按鈕,並在每個地址的末尾為每行添加了刪除模式按鈕。 我想做的是刪除每個沒有咖啡機的位置,但是首先我必須在php中進行查詢,並確定某個位置有咖啡機。 我是php的新手,所以我將不勝感激。
這是我的代碼,用於顯示數據庫表和模式按鈕的位置:
<?php
if(!isset($_SESSION['user_id'])){
header('Location: ../../index.php');
}
try{
$stmt = $conn->prepare('SELECT id,ulica,kc_broj,mjesto FROM lokacije ORDER BY id ASC');
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:error/db_error.php?err_msg');
}
}
?>
<div class="panel-heading clearfix">
<h3 style="float:left; margin:0;">Lokacije</h3>
<a href="" data-toggle="modal" data-target="#add_locModal" role="button" class="btn btn-primary btn-sm" style="float:right;">Dodaj lokaciju</a>
</div>
<table class="table table-hover">
<tr>
<th>ID</th>
<th>Ulica</th>
<th>Kućni broj</th>
<th>Mjesto</th>
<th></th>
</tr>
<?php
foreach($data as $value){
?>
<tr>
<td>
<?php echo $value['id'].'.'?>
</td>
<td>
<?php echo $value['ulica']?>
</td>
<td>
<?php echo $value['kc_broj']?>
</td>
<td>
<?php echo $value['mjesto']?>
</td>
<td align="right">
<a href="#" data-toggle="modal" data-target="#edit_loc<?php echo $value['id']?>" role="button" class="btn btn-success btn-sm edit">
<i class="fa fa-pencil-square-o"></i> Uredi
</a>
<div class="modal fade edit-u" id="edit_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content" style="text-align:left;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Uredi lokaciju broj <?php echo $value['id']?></h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<input type="hidden" name="id_lokacije" id="id_lokacije" value="<?php echo $value['id']?>">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto-u" placeholder="Ostavite prazno polje ako ne želite mjenjati naziv kavomata" value="">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="edit_loc">Spremi</button>
</div>
</div>
</div>
</div>
<a href="#" data-toggle="modal" data-target="#delete_loc<?php echo $value['id']?>" role="button" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o"></i> Izbriši
</a>
<div class="modal fade" id="delete_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="text-align:center;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h2 class="modal-title" id="myModalLabel" style="color:#a80b27;text-transform:uppercase;font-size:1.6rem;">upozorenje !</h2>
</div>
<div class="modal-body">
<h5>Da li si siguran da želiš obrisati lokaciju broj <b><?php echo $value['id']?></b>?</h5>
</div>
<div class="modal-footer">
<a href="include/locations/delete.php?id=<?php echo $value['id']?>" role="button" class="btn btn-danger">
Da, siguran sam!
</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Ne</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
?>
</table>
<div class="modal fade" id="add_locModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Dodaj novu lokaciju</h4>
</div>
<form action="" method="POST">
<div class="modal-body">
<div class="form-group ulica">
<label for="ulica">Ulica</label>
<input type="text" class="form-control" name="ulica" id="ulica" placeholder="Ulica">
</div>
<div class="form-group kc_broj">
<label for="kc_broj">Kućni broj</label>
<input type="text" class="form-control" name="kc_broj" id="kc_broj" placeholder="Kućni broj">
</div>
<div class="form-group mjesto">
<label for="mjesto">Mjesto</label>
<input type="text" class="form-control" name="mjesto" id="mjesto" placeholder="Mjesto">
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
<button type="button" class="btn btn-primary" id="add_loc">Dodaj</button>
</div>
</div>
</div>
這是我的delete.php腳本:
require '../../config/init.php';
require '../services/xss.php';
if(isset($_SESSION['user_id']) && isset($_GET['id'])){
$id = $_GET['id'];
try{
$stmt = $conn->prepare('DELETE FROM lokacije WHERE id=:id NOT IN(SELECT k.id FROM kavomati k WHERE k.id=lokacije.id)');
$stmt->bindParam(':id',$id);
$stmt->execute();
header('Location:../../coffee-locations.php');
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:../../error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:../../error/db_error.php?err_msg');
}
}
}
else{
header('Location:../../index.php');
}
?>
我嘗試了類似的方法,但是沒有用。請幫助。
由於您的查詢語法完全錯誤,因此效果不佳。 它應該是
DELETE FROM lokacije
WHERE id NOT IN(SELECT id FROM kavomati)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.