error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
require "config.php"; // Database connection details.
//////// End of Database connection /////////
//////////////////////////////////////
$id=2; // Fixed member ID for example
$count=$dbo->prepare("SELECT * FROM student WHERE id=:id");
$count->bindParam(":id", $id, PDO::PARAM_INT, 1);
if($count->execute()) {
echo " Success <br>";
$row = $count->fetch(PDO::FETCH_OBJ);
} else {
print_r($dbo->errorInfo());
}
/// Display the form to collect updated data ///
echo "<form name='myForm' action='pdo-update2.php' method='post'>
<input type='hidden' name='id' value='$id'>
<table class='t1'>
<input type='hidden' name='todo' value='change-data'>
<tr><th colspan=2>Update Profile for ID: $row->id</th></tr>
<tr class='r1'><td>Name</td><td><input type='text' name='name' value='$row->name'></td></tr>
<tr class='r1'><td>Class</td><td><input type='text' name='class' value='$row->class'></td></tr>
<tr class='r1'><td>Mark</td><td><input type='number' name='mark' value='$row->mark'></td></tr>
<tr class='r1'><td>Gender</td><td>
<input type='radio' name='gender' value='Male' " . ($row->gender == 'Male' ? 'checked' : '') . "> Male
<input type='radio' name='gender' value='Female' " . ($row->gender == 'Female' ? 'checked' : '') . "> Female
<input type='radio' name='gender' value='Others' " . ($row->gender == 'Others' ? 'checked' : '') . "> Others
</td></tr>
<tr class='r1'><td></td><td><input type='submit' value='Submit'></td></tr>
</table></form>";
We have first collected the existing data of the record by using PDO fetch object. Those data we populate as default data which user can keep like this or change.
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
require "config.php"; // Database connection details.
//////// End of Database connection /////////
// Form validation and updating data
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$name = $_POST['name'];
$class = $_POST['class'];
$mark = $_POST['mark'];
$gender = $_POST['gender'];
// Validation
if (empty($id) || empty($name) || empty($class) || empty($mark) || empty($gender)) {
echo "<div class='alert alert-danger'>All fields are required!</div>";
} elseif (!is_numeric($mark) || !is_numeric($id)) {
echo "<div class='alert alert-danger'>ID and Mark must be numbers!</div>";
} else {
try {
// Update query
$query = "UPDATE student SET name = :name, class = :class,
mark = :mark, gender = :gender WHERE id = :id";
$stmt = $dbo->prepare($query);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':class', $class);
$stmt->bindParam(':mark', $mark);
$stmt->bindParam(':gender', $gender);
if ($stmt->execute()) {
echo "<div class='alert alert-success'>
Record updated successfully for Student ID: $id</div>";
}
} catch (PDOException $e) {
echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
}
}
}
Above code will update the profile and if any validation or database problem is there then we will get the error message ( check the else condition section ).
$count=$dbo->prepare("update student set mark=80 where userid='$_SESSION[userid]'");
$count->execute();
try {
$dbo->beginTransaction(); // Start the transaction
// Prepare the update query
$stmt = $dbo->prepare("UPDATE student SET mark = :mark WHERE id = :id");
// Bind parameters
$stmt->bindParam(':mark', $mark);
$stmt->bindParam(':id', $id);
// Values for the update
$mark = 85;
$id = 3;
// Execute the update
$stmt->execute();
// Commit the transaction if successful
$dbo->commit();
echo "Transaction committed, student record updated.";
} catch (PDOException $e) {
// Rollback if any error occurs
$dbo->rollBack();
echo "Transaction failed: " . $e->getMessage();
}
if($step->execute()){
if($step->rowCount()!=1){
$db_status='NOTOK';
$msg='Record Not updated';
}else{
$msg='One record updated';
$db_status='OK';
}
}
Note that some time there may not be any change in old and new data so rowCount will return 0 only. In such case the query part remain correct but as there is no change in data so rowCount will return us 0 .
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.