What are common methods for updating MySQL entries using PHP?
When updating MySQL entries using PHP, common methods include using the UPDATE query with the SET clause to specify the columns to be updated and their new values, along with a WHERE clause to specify the condition for which rows to update.
<?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);
}
// Update a specific entry in the database
$id = 1;
$newValue = "New Value";
$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 the connection
$conn->close();
?>