WP0987 WP0987 - 7 months ago 20
HTML Question

HTML Form using php doesn't update mysql

when I change information in the form, that information should be put in $_POST. Next, the

if (isset($_POST["submit"]))
should run the query to update the table in MySQL. However, nothing gets updated in the MySQL. I'm assuming that there is a simple fix because I'm learning this.

Also, any advice about how to debug something like this would be helpful. I'm using notepad++ and the school's server.

<?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"); ?>

Answer

In your HTML form, there is no any input with name="submit" so if (isset($_POST["submit"])) this condition is false.

Add name="submit" in your submit button

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