Post düzenleme ve silme
UPDATE ve DELETE ile mevcut postları düzenleyip silersin.
Tekil yazıyı getirmek
Bir yazıyı kimliğine göre getirmek için SELECT ... WHERE id = :id sorgusu hazırlarsın. prepare ile ifadeyi oluşturur, execute([":id" => $id]) ile parametreyi bağlar ve $stmt->fetch(PDO::FETCH_ASSOC) ile tek satırı alırsın. fetch bir satır döner; satır yoksa false döner.
Güvenli bir varsayılan için ?: [] işlecini kullanırsın: satır yoksa boş dizi döner, böylece çağıran kod hata almaz.
Güncelleme ve silme yapmak
Bir yazıyı güncellemek için UPDATE posts SET title = :title, content = :content, author = :author WHERE id = :id sorgusunu prepare ile hazırlar ve execute ile çalıştırırsın. Silmek için DELETE FROM posts WHERE id = :id kullanırsın. Her iki işlemde de $stmt->rowCount() kaç satırın etkilendiğini söyler; bu değer doğrulama için kullanışlıdır.
Parametreleri her zaman execute ile bağla; değerleri dizeye gömme. Böylece kullanıcıdan gelen veri bile güvenli kalır.
Uygulama getPost, updatePost ve deletePost fonksiyonlarını yaz
Üç fonksiyon yaz. getPost(PDO $pdo, int $id): array: prepare("SELECT id, title, content, author FROM posts WHERE id = :id") yap, execute([":id" => $id]) ile çalıştır, return $stmt->fetch(PDO::FETCH_ASSOC) ?: []; döndür. updatePost(PDO $pdo, int $id, string $title, string $content, string $author): int: prepare("UPDATE posts SET title = :title, content = :content, author = :author WHERE id = :id") yap, execute([...]) ile çalıştır, return $stmt->rowCount(); döndür. deletePost(PDO $pdo, int $id): int: prepare("DELETE FROM posts WHERE id = :id") yap, execute([":id" => $id]) ile çalıştır, return $stmt->rowCount(); döndür. Demodaki çıktıyı üret.
Örnek çözümü göster
array { ... } yaz. İçinde $stmt = $pdo->prepare("SELECT id, title, content, author FROM posts WHERE id = :id"); yaz, $stmt->execute([":id" => $id]); ile çalıştır ve return $stmt->fetch(PDO::FETCH_ASSOC) ?: []; döndür. function updatePost(PDO $pdo, int $id, string $title, string $content, string $author): int { ... } yaz. İçinde $pdo->prepare("UPDATE posts SET title = :title, content = :content, author = :author WHERE id = :id"); yap, execute([...]) ile çalıştır ve return $stmt->rowCount(); döndür. function deletePost(PDO $pdo, int $id): int { ... } yaz. İçinde $pdo->prepare("DELETE FROM posts WHERE id = :id"); yap, execute([":id" => $id]); ile çalıştır ve return $stmt->rowCount(); döndür.
Örnek çözüm:
<?php
// Blog projesi: BlogPost + setupDatabase + createPost + getAllPosts + getPost + updatePost + deletePost
class BlogPost {
public string $title = "";
public string $content = "";
public string $author = "";
}
function setupDatabase(): PDO {
$pdo = new PDO("sqlite::memory:");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
author TEXT NOT NULL
)");
return $pdo;
}
function createPost(PDO $pdo, string $title, string $content, string $author): int {
$stmt = $pdo->prepare("INSERT INTO posts (title, content, author) VALUES (:title, :content, :author)");
$stmt->execute([":title" => $title, ":content" => $content, ":author" => $author]);
return (int) $pdo->lastInsertId();
}
function getAllPosts(PDO $pdo): array {
$stmt = $pdo->query("SELECT id, title, content, author FROM posts ORDER BY id");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getPost(PDO $pdo, int $id): array {
$stmt = $pdo->prepare("SELECT id, title, content, author FROM posts WHERE id = :id");
$stmt->execute([":id" => $id]);
return $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
}
function updatePost(PDO $pdo, int $id, string $title, string $content, string $author): int {
$stmt = $pdo->prepare("UPDATE posts SET title = :title, content = :content, author = :author WHERE id = :id");
$stmt->execute([":title" => $title, ":content" => $content, ":author" => $author, ":id" => $id]);
return $stmt->rowCount();
}
function deletePost(PDO $pdo, int $id): int {
$stmt = $pdo->prepare("DELETE FROM posts WHERE id = :id");
$stmt->execute([":id" => $id]);
return $stmt->rowCount();
}
// Demo:
$pdo = setupDatabase();
createPost($pdo, "İlk yazım", "Merhaba", "Ayşe");
createPost($pdo, "İkinci yazım", "Selam", "Alex");
$post = getPost($pdo, 1);
echo $post['title'] . " - " . $post['author'] . "\n";
updatePost($pdo, 1, "Güncellenmiş yazım", "Yeni içerik", "Ayşe");
echo "Güncellendi\n";
$deleted = deletePost($pdo, 2);
echo "Silindi: " . $deleted . "\n";
$posts = getAllPosts($pdo);
foreach ($posts as $p) {
echo $p['title'] . " - " . $p['author'] . "\n";
}
Kendini test et
-
UPDATE ve DELETE kavramlarını eşleştir
- UPDATE ... SET
- Satırı günceller
- DELETE FROM
- Satırı siler
- WHERE id = :id
- Belirli satırı hedefler
- ->rowCount
- Etkilenen satır sayısı
-
UPDATE ve DELETE sorgularında WHERE neden önemlidir?
- Tüm satırları etkilemek için
- Sorguyu hızlandırmak için
- Tablo adını belirtmek için
- Yalnızca belirli bir satırı hedeflemek için
Cevabı göster
Yalnızca belirli bir satırı hedeflemek için — Doğru. WHERE olmadan UPDATE/DELETE tüm satırları etkiler; bu genellikle istenmez.
-
UPDATE sorgusunu tamamla
$stmt = $pdo->prepare("____");Cevabı göster
$stmt = $pdo->prepare("UPDATE posts SET title = :title WHERE id = :id");