Tags
Asked 3 years ago
17 Jun 2021
Views 221
Esteban

Esteban posted

Update data present in database by php

Update data present in database by php
sec8

sec8
answered Apr 28 '23 00:00


To update data present in a MySQL database using PHP, you can use an UPDATE SQL statement with the mysqli_query () function. 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());
}

// Update a record in the "users" table
$sql = "UPDATE users SET name='John Doe', email='john.doe@example.com' WHERE id=1";

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 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.
Post Answer