dilip
answered Apr 28 '23 00:00
To update table data via PHP/MySQL, you can use an UPDATE SQL statement with the mysqli_query() function in PHP. Here's an example code snippet that shows how to update a record in a table named "users":
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Define variables for the new data
$id = 1; // Replace with the ID of the record you want to update
$name = "John Doe";
$email = "john.doe@example.com";
// Update the record in the "users" table
$sql = "UPDATE users SET name='$name', email='$email' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
// Close the database connection
mysqli_close($conn);
?>
In the above code, we first connect to the database using mysqli_connect (). Then, we define variables for the new data that we want to update in the record. After that, we execute an SQL UPDATE statement using mysqli_query(). The WHERE clause specifies which record to update based on the id field. If the query is successful, we print a success message; otherwise, we print an error message.
Note that you should replace "username", "password", " database ", "users", "name", "email", and "id" with your own values. Also, don't forget to close the database connection using mysqli_close() after you're done with it.