Update Record From a MySQL Database using PHP


Table Name tbl_student_record

ID Name Class Subject
1 Mohan 2nd Math

Object-Oriented

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// In this line  Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE tbl_student_record SET name='Rahul Sharma',class='3rd',subject='Art' WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?> 

Procedural

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// In this line  Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = mysqli_query($conn,"UPDATE tbl_student_record SET name='Rahul Sharma',class='3rd',subject='Art' WHERE id=1")or die(mysqli_error());

if ( mysqli_affected_rows($conn) >0) {
    echo "Record has been updated successfully";
} else {
    echo "Record has not been updated successfully: ";
}

mysqli_close($conn);
?> 

After Update Record output
ID: 1 Name: Rahul Sharma Class: 3rd Subject: Art