php - 从 MySQL DB 中删除,表单不会在 PHP 中显示

标签 php html css bootstrap-4

我不知道我做错了什么,但我的表单没有显示。

代码如下:

删除后.php:

<?php

session_start();

$username=$_SESSION['uname'];

// Process delete operation after confirmation
if(isset($_POST["pid"]) && !empty($_POST["pid"])){

    $cn=mysqli_connect("localhost", "root", "", "imedtalks");
    
    // Prepare a delete statement
    $sql = "DELETE FROM posts WHERE pid = ?";
    
    if($stmt = mysqli_prepare($cn, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_pid);
        
        // Set parameters
        $param_pid = trim($_POST["pid"]);
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records deleted successfully. Redirect to landing page
            header("location: CAposts.php");
            exit();
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    
    // Close connection
    mysqli_close($cn);
} else{
    // Check existence of id parameter
    if(empty(trim($_GET["pid"]))){
        // URL doesn't contain id parameter. Redirect to error page
        header("location: CAposts.php");
        exit();
    }
}
?>




  <html>

  <head>
    <title>Delete PostID-
      <?php echo $_GET["pid"];?>
    </title>

    <link href="./css/bootstrap.min.css" rel="stylesheet" />
    <script src="./scripts/jquery-3.3.1.min.js"></script>
    <script src="./scripts/bootstrap.min.js"></script>


    <style>
      
      .wrapper {
        width: 500px;
        margin: 0 auto;
      }
    </style>

  </head>

  <body>


    <div class="wrapper">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12">
            <div class="page-header">
              <h1 class="text-center">Delete Post</h1>
            </div>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
              <div class="alert alert-danger fade">
                <input type="hidden" name="pid" value="<?php echo trim($_GET[" pid "]); ?>"/>
                <div>Are you sure you want to delete this record?</div><br>
                <div>
                  <button type="submit" name="yes" class="btn btn-danger">Yes</button>
                  <a href="CAposts.php"><button type="button" name="no" class="btn btn-primary">No</button></a>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

  </body>

  </html>

在这里,我正在尝试使用 pid 删除一行,它是从 url 中检索的,即从上一页传递的。

我只想在用户选择时显示一个警报,如果按下"is"按钮,将再次调用该页面以执行 sql 查询。

但是,我不知道为什么,只是标题“删除帖子”出现在那里,并且表单被跳过。

我在这里使用的是 Bootstrap 4。

最佳答案

这是因为你没有在使用前初始化变量。

把这个声明:

$param_pid = trim($_POST["pid"]);

之前:

mysqli_stmt_bind_param($stmt, "i", $param_pid);

关于php - 从 MySQL DB 中删除,表单不会在 PHP 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55410322/

相关文章:

php - 如何检查php中变量的内存位置?

php - PHP 如何准确地回显 MySQL 字段中存储的内容?

html - 更新 angular.min.js v1.6.9 后出现 Angular ​​度摘要周期问题

html - Css:带有固定标题和多个 tbody 的滚动表

html - 像在歌曲簿中一样自定义上标

PHP 数组键更改

html - 包裹但保持列结构的动态宽度 flexboxes?

javascript - 修复 Bootstrap 4 中的页脚?

html - 如何使用显示 : flex? 在包装列中进行垂直滚动

php - 在 Laravel 5 中使用自定义时间戳返回错误