How can PHP be used to update a SQL database record with input values?
To update a SQL database record with input values using PHP, you can use SQL UPDATE query along with PHP variables to set the new values for the record. First, establish a database connection using PHP, then retrieve the input values from a form or any other source. Finally, construct and execute an SQL UPDATE query to update the record with the new input values.
<?php
// Establish a database connection
$conn = new mysqli("localhost", "username", "password", "database");
// Retrieve input values
$id = $_POST['id'];
$newValue = $_POST['new_value'];
// Construct and execute SQL UPDATE query
$sql = "UPDATE table_name SET column_name = '$newValue' WHERE id = $id";
$result = $conn->query($sql);
if ($result === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Related Questions
- How can PHP developers ensure that both the sender and receiver of encrypted data have the necessary keys for decryption?
- How can PHP developers optimize their database design to avoid the need for complex string manipulation in SQL queries?
- What are the potential pitfalls of using absolute path instead of relative path in the form tag to prevent the session ID from being visible in the URL?