记录不存在时显示消息

show message when record doesnt exist

我有一个数据库和一个带有文本框的网站。用户可以输入数据库中记录的名称,然后它将被删除。我想添加但不知道如何添加的是查询检查名称是否存在于数据库中然后将其删除的方法,如果不存在则应显示一条错误消息。使用我现在拥有的代码,我始终可以看到错误消息。

<div class="main_content">
        <div class="header">Bestanden Verwijderen</div>
        <div class="info">
            <div>
            <form action="delete.php" method="post">
                <label for="id">Bestand Naam</label>                    
                <input type="text" name="name"  id="name">
                <input type="submit" value="Delete">
            </form>


            <?php

                $msg = '';
                // Check if POST data is not empty
                if (!empty($_POST)) {
                    // Post data not empty insert a new record
                    // Set-up the variables that are going to be inserted, we must check if the POST variables exist if not we can default them to blank
                    $name = isset($_POST['name']) && !empty($_POST['name']) && $_POST['name'] != 'auto' ? $_POST['name'] : NULL;
                    // Check if POST variable "name" exists, if not default the value to blank, basically the same for all variables
                    $name = isset($_POST['name']) ? $_POST['name'] : '';                        
                }

                $sql = "DELETE FROM projects WHERE name = :name";
                $stmt = $conn->prepare($sql);
                $stmt->bindParam(':name', $name, PDO::PARAM_STR);
                $stmt->execute();
                $count = $stmt->rowCount();// check affected rows using rowCount
                if ($count > 0) {
                    echo 'Bestand met de naam ' . $name . 'is verwijderd.';
                } elseif ($count == 0) {
                    echo "Bestand met die naam bestaat niet";
                }


            ?>
            </div>
            <div>
            </div>
            <div></div>
            <div></div>
        </div>
    </div>
</div>

if ($count > 0) {

结束后关闭 if if (!empty($_POST)) {
$msg = '';
// Check if POST data is not empty
if (!empty($_POST)) {
    // Post data not empty insert a new record
    // Set-up the variables that are going to be inserted, we must check if the POST variables exist if not we can default them to blank
    $name = isset($_POST['name']) && !empty($_POST['name']) && $_POST['name'] != 'auto' ? $_POST['name'] : NULL;
    // Check if POST variable "name" exists, if not default the value to blank, basically the same for all variables
    $name = isset($_POST['name']) ? $_POST['name'] : '';                        

    $sql = "DELETE FROM projects WHERE name = :name";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();// check affected rows using rowCount
    if ($count > 0) {
        echo 'Bestand met de naam ' . $name . 'is verwijderd.';
    } elseif ($count == 0) {
        echo "Bestand met die naam bestaat niet";
    }
}

如果发送 POST 请求,您只会填充 $name,但始终会发送 SQL 查询。

发送查询时,$name 未定义,因此要么 namenull 进行检查,要么完全失败。因此,受影响的行数始终为 0。

所以你需要做的是将你的 if (!empty($_POST)) { 一直延伸到最后:

if (!empty($_POST)) {
    // Post data not empty insert a new record
    // Set-up the variables that are going to be inserted, we must check if the POST variables exist if not we can default them to blank
    $name = isset($_POST['name']) && !empty($_POST['name']) && $_POST['name'] != 'auto' ? $_POST['name'] : NULL;
    // Check if POST variable "name" exists, if not default the value to blank, basically the same for all variables
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    $sql = "DELETE FROM projects WHERE name = :name";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();// check affected rows using rowCount
    if ($count > 0) {
        echo 'Bestand met de naam ' . $name . 'is verwijderd.';
    } elseif ($count == 0) {
        echo "Bestand met die naam bestaat niet";
    }                        
}

首先,

  • 检查该名称是否存在于数据库中。如果是,那么

  • 删除。

    if ( !empty ( $_POST ) ) {

      $name = (isset($_POST['name']) && !empty($_POST['name']) && $_POST['name'] != 'auto') ? $_POST['name'] : NULL;
    
      //Check if it exist in the database
      $query = "SELECT * FROM projects WHERE name = :name";
      $stmnt = $conn->prepare($query);
      $stmnt->bindParam(':name', $name);
      $stmnt->execute();
      $rowCount = $stmnt->rowCount();
      if ($rowCount > 0){
          $sql = "DELETE FROM projects WHERE name = :name";
          $stmt = $conn->prepare($sql);
          $stmt->bindParam(':name', $name, PDO::PARAM_STR);
          $stmt->execute();
          $count = $stmt->rowCount()
          if ($count > 0) {
              echo 'The name ' . $name . ' has been deleted.';
          } 
      } else {
          echo 'The name ' . $name . ' was not found!';
          exit;
      }
    

    }

谢谢。