SyntaxStudy
Sign Up
PHP UPDATE and DELETE with PDO
PHP Intermediate 4 min read

UPDATE and DELETE with PDO

PDO UPDATE and DELETE

UPDATE and DELETE work like INSERT — prepare a statement with placeholders and execute with an array of values. Check rowCount() to verify rows were affected.

Example
<?php
// UPDATE
$stmt = $pdo->prepare("UPDATE users SET name = :name, email = :email WHERE id = :id");
$stmt->execute(["name" => $newName, "email" => $newEmail, "id" => $userId]);
echo "Updated " . $stmt->rowCount() . " rows";

// DELETE
$stmt = $pdo->prepare("DELETE FROM sessions WHERE user_id = :uid AND expires_at < NOW()");
$stmt->execute(["uid" => $userId]);
echo "Deleted " . $stmt->rowCount() . " expired sessions";

// Check if any row was affected
if ($stmt->rowCount() === 0) {
    throw new NotFoundException("Record not found");
}
Pro Tip

Check rowCount() after UPDATE/DELETE to confirm the operation actually affected rows — a 0 usually means the WHERE clause matched nothing.