【问题标题】:Delete from MySQL DB, Form does't get displayed in PHP从 MySQL 数据库中删除,表单不会在 PHP 中显示
【发布时间】:2019-08-19 23:24:10
【问题描述】:

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

代码如下:

PostDelete.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 删除一行,该 pid 是从 url 检索的,即从上一页传递的。

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

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

我在这里使用的是 Bootstrap 4。

【问题讨论】:

  • 您的服务器是否显示错误? &lt;?php echo $_GET["pid"];?&gt; 将生成警告,因为它没有设置。
  • 不,它没有显示任何错误,它可以工作,显示 pid。
  • 源列表向您展示了什么?

标签: php html css bootstrap-4


【解决方案1】:

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

把这个声明:

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

之前:

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

【讨论】:

  • 它工作正常,没有错误,但我也试过你的方法,但它不会影响结果输出。还是一样,只是直到html部分的h1标签显示出来,不知道,但是表单仍然丢失。
  • 请尝试调试。打印$_POST['pid]、打印SQL、打印sql结果等等……应该可以了。如果你调试代码,这没什么大不了的。
猜你喜欢
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多