You can use PHP to Dynamically create an HTML form from a MySQL record and then be able to edit the record.
Warning: Your Web Browser may cache form values. Opening a new tab may work if refreshing a page does not.
phpUpdateForm.php
<?php
$student_id=10;
$servername = "localhost";
$username = "bob";
$password = "123456";
$db = "classDB";
$conn = new mysqli($servername, $username, $password, $db);
if($conn->connect_error){
die("Connection failed ".$conn->connect_error);
}
$sql = "select * from students where student_id='$student_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
$row = $result->fetch_assoc();
$name = $row["name"];
$age = $row["age"];
$gender = $row["gender"];
$uniform = $row["uniform"];
echo
"<html>
<body>
<form action='phpUpdateFormScript.php' method='post'>
Student ID: $student_id<br>
<input type='hidden' name='student_id' value='$student_id'>
Uniform: $uniform<br>
Name: <input type='text' name='name' value='$name'><br>
Age: <input type='text' name='age' value='$age'><br>
Gender: <select name='gender'>
<option value='$gender' selected>$gender </option>
<option value='boy'>Boy</option>
<option value='girl'>Girl</option>
</select><br>
<input type ='submit'>
</form>
</body>
</html>";
} else {
echo "Not Found";
}
$conn->close();
?>
phpUpdateFormScript.php
<?php
$student_id = $_POST["student_id"];
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$servername = "localhost";
$username = "bob";
$password = "123456";
$db = "classDB";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
$sql = "update students set name='$name', age='$age', gender='$gender' where student_id='$student_id'";
if ($conn->query($sql) === TRUE) {
echo "Records updated: ".$student_id."-".$name."-".$age."-".$gender;
} else {
echo "Error: ".$sql."<br>".$conn->error;
}
$conn->close();
?>
Be the first to comment