php - 数据库行更新失败,PHP 没有任何错误

标签 php html css bootstrap-4

我正在将数据库中的值检索到表单中以进行更新,按下提交按钮。 确实检索了值,但更新过程失败且没有任何错误。

代码如下:

<?php
session_start();

$username=$_SESSION['uname'];


$cn=mysqli_connect("localhost", "root", "", "testdb");
 
// Define variables and initialize with empty values
$course = $category = "";
$title = $descp = "";
 
// Processing form data when form is submitted
if(isset($_POST["pid"]) && !empty($_POST["pid"])){
    // Get hidden input value
    $pid = $_POST["pid"];

    
    // Check input errors before inserting in database
    if(empty($course) && empty($category) && empty($title) && empty($descp)){
        // Prepare an update statement
        $sql = "UPDATE posts SET course=?, category=?, title=?, descp=? WHERE pid=?";
         
        if($stmt = mysqli_prepare($cn, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ssssi", $param_course, $param_category, $param_title, $param_descp, $param_pid);
            
            // Set parameters
            $param_course = $course;
            $param_category = $category;
            $param_title = $title;
            $param_descp = $descp;
            $param_pid = $pid;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records updated successfully. Redirect to landing page
                header("location: CAposts.php");
                exit();
            } else{
                echo "Something went wrong. Please try again later.";
            }

        // Close statement
        mysqli_stmt_close($stmt);
        }
         

    }
    
    // Close connection
    mysqli_close($cn);
} else{
    // Check existence of id parameter before processing further
    if(isset($_GET["pid"]) && !empty(trim($_GET["pid"]))){
        // Get URL parameter
        $pid =  trim($_GET["pid"]);
        
        // Prepare a select statement
        $sql = "SELECT * 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 = $pid;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                $result = mysqli_stmt_get_result($stmt);
    
                if(mysqli_num_rows($result) == 1){
                    /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
                    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                    
                    // Retrieve individual field value
                    $pid = $row['pid'];
                    $uname = $row['uname'];
                    $course = $row['course'];
                    $category = $row['category'];
                    $pdate = $row['pdate'];
                    $title = $row['title'];
                    $descp = $row['descp'];

                } else{
                    // URL doesn't contain valid id. Redirect to error 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{
        // URL doesn't contain id parameter. Redirect to error page
        header("location: CAposts.php");
        exit();
    }
}
?>


  <html>

  <head>
    <title>IMEDTalks-Post-
      <?php echo $title;?>
    </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>
      /* Make the image fully responsive */
      
      .carousel-inner img {
        width: 100%;
        height: 30%;
      }
    </style>

  </head>

  <body>

    <div class="wrapper">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12">
            <div class="page-header">
              <h2 class="text-center">Update Post</h2>
            </div>
            <p class="text-center">Please edit the input values and submit to update the post.</p>
            <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">


              <div class="form-group">
                <div class="row">
                  <label class="col-form-label col-md-1 offset-3" for="course">Course:</label>
                  <div class="col-md-2">
                    <select name="course" class="form-control" required>
                      <option value="<?php echo $course;?>" selected>
                        <?php echo $course;?>
                      </option>
                      <option value="">Choose any:</option>
                      <option value="comp">Comp</option>
                      <option value="theo">Theory</option>
                    </select>
                  </div>
                  <label class="col-form-label col-md-1" for="category">Category:</label>
                  <div class="col-md-3">
                    <select name="category" class="form-control" required>
                      <option value="<?php echo $category;?>" selected>
                        <?php echo $category;?>
                      </option>
                      <option value="">Choose any:</option>
                      <option value="plang">Programming Language</option>
                      <option value="web">Web Technologies</option>
                      <option value="maths">Mathematics and Statistics</option>

                      <option value="others">Others</option>
                    </select>
                  </div>
                </div>
              </div>

              <div class="form-group row">
                <label for="title" class="col-form-label col-md-2">Title:
                        </label>
                <div class="col-md-10">
                  <input type="text" class="form-control" value="<?php echo $title;?>" name="title" required>
                </div>
              </div>


              <div class="form-group row">
                <label for="desc" class="col-form-label col-md-12">Description:
                        </label>
                <div class="col-md-12">
                  <textarea class="form-control" name="descp" rows="20" required><?php echo $descp;?></textarea>
                </div>
              </div>

              <input type="hidden" name="pid" value="<?php echo $pid;?>" />


              <div class="form-group row">
                <div class="col-md-4 offset-4">
                  <a href="CAposts.php"><button type="button" name="cancel" 
                            class="btn-lg btn-danger">Cancel</button></a>
                </div>

                <div class="col-md-4">
                  <button type="submit" name="update" class="btn-lg btn-success">Submit</button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>


  </body>

  </html>

附言: pid 是从上一页以表格格式列出数据的页面购买的,单击按钮后,该数据/帖子将加载到表单中以使用 pid 进行编辑,pid 是我的数据库表中的主键。 使用 Bootstrap 4。

在第一次评论后编辑。

最佳答案

您的查询中有 5 列,但您只绑定(bind)了其中的 4 列,因此您忘记了 s

$sql = "UPDATE posts SET course=?, category=?, title=? descp=? WHERE pid=?";

    if($stmt = mysqli_prepare($cn, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "sssi", $param_course, $param_category, $param_title, $param_descp, $param_pid);

这里有一个更清晰的更新代码:

$stmt = $conn->prepare("UPDATE posts SET course=?, category=?, title=?, descp=? WHERE pid=?");
$stmt->bind_param("ssssi", $course, $category, $title, $descp, $pid);
$stmt->execute();

关于php - 数据库行更新失败,PHP 没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55382115/

相关文章:

带闭包的 PHPUnit 测试

PHP 错误行号完全关闭

HTML/CSS - 移动友好且易于调整大小

html - 将 html 链接到 css 在 Google Chrome 上不起作用

php - mysql不会插入5到10个用户同时提交的同一个php表单的所有数据

jquery - 悬停时CSS伸缩边框动画

html - 使用 CSS 网格突破

html - Recaptcha固定图像大小

javascript - 带有 cookie 的 CSS 主体类 jquery 切换器

php - 查询未插入数据没有错误