How can HTML form inputs be saved and edited later using PHP and MySQL?
To save and edit HTML form inputs using PHP and MySQL, you need to first capture the form data submitted by the user, then store it in a MySQL database. To edit the data later, you can retrieve the saved data from the database, populate the form fields with this data, and allow the user to make changes before updating the database.
// Save form data to MySQL database
// Assuming you have established a database connection
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
// Insert data into MySQL database
$sql = "INSERT INTO table_name (column1, column2) VALUES ('$input1', '$input2')";
$result = mysqli_query($conn, $sql);
}
// Retrieve data from MySQL database for editing
// Assuming you have established a database connection
$sql = "SELECT * FROM table_name WHERE id = $id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// Populate form fields with retrieved data
<input type="text" name="input1" value="<?php echo $row['column1']; ?>">
<input type="text" name="input2" value="<?php echo $row['column2']; ?>">
Related Questions
- What is the purpose of using isset($_GET['strom-test']) in PHP and how does it affect variable assignment?
- In PHP, what are some alternative methods to achieve array rotation and key reassignment for ASCII encoding beyond the provided examples?
- What are some alternative methods to Select2 and jQuery for enhancing user input in PHP forms with large datasets?