How can PHP be used to update database entries based on user input from a form?
To update database entries based on user input from a form using PHP, you can use the $_POST superglobal to retrieve the user input values from the form fields. Then, you can construct an SQL query to update the database with the new values based on the user input.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get user input from form
$id = $_POST['id'];
$newValue = $_POST['new_value'];
// Update database entry based on user input
$sql = "UPDATE table_name SET column_name = '$newValue' WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
// Close database connection
$conn->close();
?>