使用php的HTML表单不会更新mysql

时间:2016-04-21 05:19:36

标签: php html mysql

当我更改表单中的信息时,该信息应放在$ _POST中。接下来,if (isset($_POST["submit"]))应运行查询以更新MySQL中的表。但是,MySQL中没有任何更新。我假设有一个简单的修复,因为我正在学习这个。

此外,任何有关如何调试此类内容的建议都会有所帮助。我正在使用notepad ++和学校的服务器。

<?php require_once("session.php"); ?>
<?php 
  require_once("included_functions.php");
  new_header("Here is Who's who!", "CRUD/editPeople.php"); 
  $mysqli = db_connection();
  if (($output = message()) !== null) {
     echo $output;
  }

  if (isset($_POST["submit"])) {
     $ID = $_GET["id"];
     // UPDATE query on $ID
     $query = "UPDATE people SET ";
     $query .= "FirstName = '".$_POST["FirstName"];
     $query .= "', LastName = '".$_POST["LastName"];
     $query .= "', Birthdate = '".$_POST["Birthdate"];
     $query .= "', BirthCity = '".$_POST["BirthCity"];
     $query .= "', BirthState = '".$_POST["BirthState"];
     $query .= "', Region = '".$_POST["Region"]."' ";
     $query .= "WHERE PersonID = {$ID}";
     //Output query results and return to readPeople.php
     $result = $mysqli->query($query);

     if($result) {
       $_SESSION["message"] = $_POST["FirstName"]." ".$_POST["LastName"]." has been changed";
     } else {
       $_SESSION["message"] = "Error! Could not change ".$_POST["FirstName"]." ".$_POST["LastName"];
     }
     //Once the Edit has been completed (CHANGE button clicked)
     //redirect to the readPeople.php webpage
     header("Location: readPeople.php");
     exit;
   } else { 
  // GET id and create a query to SELECT * on the id
      if (isset($_GET["id"]) && $_GET["id"] !== "") {
      // Get id
      $ID = $mysqli->real_escape_string(trim($_GET['id']));

      $query = "SELECT * FROM people WHERE ";
      $query .= "PersonID = '".$ID."'";
      $result = $mysqli->query($query);
      //Process query
      if ($result && $result->num_rows > 0) {
         $row = $result->fetch_assoc();
         echo "<div class='row'>";
         echo "<label for='left-label' class='left inline'>";
         echo "<h3>".$row["FirstName"]." ".$row["LastName"]."'s Profile</h3>";

         // Create form with inputs for each field in people table
         echo "<p><form action = 'editPeople.php?id={$ID}' method='post'>";
         echo "<p><input type = 'text' name = 'FirstName' value = '".
             $row["FirstName"]."'/></p>";
         echo "<p><input type = 'text' name = 'LastName' value = '".
             $row["LastName"]."'/></p>";
         echo "<p><input type = 'text' name = 'Birthdate' value = '".
             $row["Birthdate"]."'/></p>"; 
         echo "<p><input type = 'text' name = 'BirthCity' value = '".
             $row["BirthCity"]."'/></p>";
         echo "<p><input type = 'text' name = 'BirthState' value = '".
             $row["BirthState"]."' /></p>";
         echo "<p><input type = 'text' name = 'Region' value = '".
             $row["Region"]."' /></p>";

         echo "<p><input type='submit' value='Update' /></p>";  
         echo "</form>";
    echo "<br /><p>&laquo:<a href='readPeople.php'>Back to Main Page</a>";
    echo "</label>";
    echo "</div>";
    }
         //Query failed to exit. Return to readPeople.php and output error
         else {
         $_SESSION["message"] = "Person could not be found!";
         header("Location: readPeople.php");
         exit;
    }}}
?>
<?php  new_footer("Who's Who"); ?>

3 个答案:

答案 0 :(得分:3)

在您的HTML表单中,name="submit"没有任何输入,因此if (isset($_POST["submit"]))此条件为false。

在提交按钮中添加name="submit"

echo "<p><input type='submit' name='submit' value='Update' /></p>";  

答案 1 :(得分:1)

要调试并找到传递的内容,我使用print_r($_POST)print_r($_GET)

我在大多数使用表单的脚本中使用它,以控制传递的内容。但当然只在开发服务器上。

答案 2 :(得分:0)

您的按钮没有名称。把名字=&#34;提交&#34;按钮。因此,isset($ _ POST [&#39; SUBMIT&#39;])无效。