【问题标题】:show message when record doesnt exist记录不存在时显示消息
【发布时间】:2021-11-11 06:39:34
【问题描述】:

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

<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 检查是否这是一个 POST 请求……?甚至 DELETE 语句本身都没有被包装到那里 - 如果您甚至没有得到任何 POST 参数,执行该语句会有什么意义?

标签: php sql pdo crud


【解决方案1】:

if ($count &gt; 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";
    }
}

【讨论】:

    【解决方案2】:

    只有在发送 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";
        }                        
    }
    

    【讨论】:

      【解决方案3】:

      首先,

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

      • 删除它。

        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;
          }
        

        }

      谢谢。

      【讨论】:

        猜你喜欢
        • 2018-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        相关资源
        最近更新 更多