如何使用PDO和限制

时间:2016-12-13 10:32:23

标签: php mysql pdo

我在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">&times;</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">&times;</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">&times;</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');
 }
?>

我试过这样的东西,但它不起作用。请帮助。

1 个答案:

答案 0 :(得分:0)

它不应该工作,因为您的查询语法完全错误。它应该是

DELETE FROM lokacije 
WHERE id NOT IN(SELECT id FROM kavomati)