What is the correct way to update individual records in a database using PHP?
When updating individual records in a database using PHP, you need to use SQL UPDATE queries to modify specific fields in the database table. You will need to specify the table name, the fields to update, and the condition to identify the specific record 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 individual record
$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 connection
$conn->close();
?>