PHP:MySQL数据库表中的更新在各列中设置了空白值

时间:2019-03-29 08:12:03

标签: php mysql mysqli

我正在从数据库中检索值以进行更新,在按下提交按钮时,值应该会更新。

代码如下:

PostUpdate.php:

<?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(数据库表中的主键) 我使用引导程序4。

我面临的问题:

使用pid进行更新操作没有任何错误,但是在执行此更新操作后,数据库表中的课程,类别,标题,描述的值将设置为空白。

我不知道这里出了什么问题。

0 个答案:

没有答案