require "config.php";// Database connection file,$connection object
$query="UPDATE student SET class='Four'";
if ($connection->query($query)) {
echo "Records Updated";
}else{
echo $connection->error;
}
With number of rows updated
$query="UPDATE student SET class='Four'";
if ($connection->query($query)) {
echo "Records Updated : ";
echo $connection->affected_rows;
}else{
echo $connection->error;
}
With Parameters by using bind_param()
<?Php
require "config.php";// Database connection file.
$class='Three';
$mark=66;
$id=5;
$query="UPDATE student SET class=?,mark=? WHERE id=?";
$stmt = $connection->prepare($query);
if ($stmt) {
$stmt->bind_param('sii', $class, $mark, $id);
$stmt->execute();
echo "Record Updated:";
echo $stmt->affected_rows;
}else{
echo $connection->error;
}
?>
Procedural style
$class='Three';
$mark=68;
$id=5;
$query="UPDATE student SET class=?,mark=? WHERE id=?";
if ($stmt = mysqli_prepare($connection,$query)){
mysqli_stmt_bind_param($stmt, "sii", $class, $mark, $id);
mysqli_stmt_execute($stmt);
echo "Record Updated:";
echo mysqli_affected_rows($connection);
}else{
echo mysqli_error($connection);
}
$class = 'Three';
$mark = 5;
// SQL UPDATE query with place holders
$query = "UPDATE student SET mark=mark+? WHERE class=?";
// Prepare the SQL statement
if ($stmt = $connection->prepare($query)) {
// Bind parameters (i = integer,s = string)
$stmt->bind_param("is", $mark,$class);
// Execute the statement
if ($stmt->execute()) {
// Output the number of affected rows
echo "Records Updated: " . $connection->affected_rows;
} else {
// Handle execution error
throw new Exception("Statement execution failed: " . $stmt->error);
}
// Close the statement
$stmt->close();
} else {
// Handle preparation error
throw new Exception("Statement preparation failed: " . $connection->error);
}
Output
Records Updated: 4
try {
// Variables to update
$class = 'Three';
$mark = 68;
$id = 5;
// SQL UPDATE query
$query = "UPDATE student SET class=?, mark=? WHERE id=?";
// Prepare the statement
if ($stmt = mysqli_prepare($connection, $query)) {
// Bind parameters (s = string, i = integer)
mysqli_stmt_bind_param($stmt, "sii", $class, $mark, $id);
// Execute the statement
if (mysqli_stmt_execute($stmt)) {
// Output the number of affected rows
echo "Record Updated: " . mysqli_affected_rows($connection);
} else {
// Handle execution error
throw new Exception("Statement execution failed: " . mysqli_stmt_error($stmt));
}
// Close the statement
mysqli_stmt_close($stmt);
} else {
// Handle preparation error
throw new Exception("Statement preparation failed: " . mysqli_error($connection));
}
} catch (Exception $e) {
// Handle any caught errors
echo "Error: " . $e->getMessage();
}
// Close the connection
mysqli_close($connection);
Here’s the object-oriented style (OOP) version of the code with a try-catch block for error handling:
try {
// Variables to update
$class = 'Three';
$mark = 68;
$id = 5;
// SQL UPDATE query
$query = "UPDATE student SET class=?, mark=? WHERE id=?";
// Prepare the SQL statement
if ($stmt = $connection->prepare($query)) {
// Bind parameters (s = string, i = integer)
$stmt->bind_param("sii", $class, $mark, $id);
// Execute the statement
if ($stmt->execute()) {
// Output the number of affected rows
echo "Record Updated: " . $connection->affected_rows;
} else {
// Handle execution error
throw new Exception("Statement execution failed: " . $stmt->error);
}
// Close the statement
$stmt->close();
} else {
// Handle preparation error
throw new Exception("Statement preparation failed: " . $connection->error);
}
} catch (Exception $e) {
// Handle any caught exceptions
echo "Error: " . $e->getMessage();
}
// Close the connection
$connection->close();
$connection->begin_transaction();
$class = 'Three';
$mark = 50;
// Prepare the SQL query
$query = "UPDATE student SET mark=? WHERE class=?";
if ($stmt = $connection->prepare($query)) {
$stmt->bind_param("is", $mark,$class);
if ($stmt->execute()) {
echo "Record updated successfully!";
echo "<br>Number of records Updated: " . $connection->affected_rows;
$connection->commit(); // Commit changes if successful
} else {
$connection->rollback(); // Rollback if there's an error
echo "Error during update, transaction rolled back.";
}
$stmt->close();
}
Output
Record updated successfully!
Number of records Updated: 5
$query = "UPDATE student SET class=?, mark=? WHERE id=? AND gender=?";
if ($stmt = $connection->prepare($query)) {
$stmt->bind_param("sisi", $class, $mark, $id, $gender);
$stmt->execute();
echo "Updated records: " . $stmt->affected_rows;
$stmt->close();
}
$id=5;
$mark=70;
$query = "UPDATE student SET mark=? WHERE id=?";
if ($stmt = $connection->prepare($query)) {
$stmt->bind_param("ii", $mark, $id);
if ($stmt->execute()) {
$stmt->close();
// Now fetch the updated record
$result = $connection->query("SELECT * FROM student WHERE id=$id");
$updatedRecord = $result->fetch_assoc();
echo "Updated Record: " . json_encode($updatedRecord);
}
}

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.