📘 Lesson · Lesson 17
PHP CRUD Operations
PHP CRUD Operations
What is CRUD?
CRUD = Create, Read, Update, Delete — the four basic database operations every web app needs. Here is each one with PDO.
Create (Insert)
<?php
$stmt = $pdo->prepare("INSERT INTO students (name, marks) VALUES (?, ?)");
$stmt->execute(["Aman", 88]);
?>
Read (Select)
<?php
$stmt = $pdo->query("SELECT * FROM students");
foreach ($stmt as $row) {
echo $row["name"] . " - " . $row["marks"] . "<br>";
}
?>
Update
<?php
$stmt = $pdo->prepare("UPDATE students SET marks = ? WHERE id = ?");
$stmt->execute([95, 1]);
?>
Delete
<?php
$stmt = $pdo->prepare("DELETE FROM students WHERE id = ?");
$stmt->execute([5]);
?>
Summary
- CRUD = Create (INSERT), Read (SELECT), Update, Delete.
- Use prepared statements for all four to stay secure.
CRUD क्या है?
CRUD = Create, Read, Update, Delete — हर web app को चाहिए ये चार basic database operations। यहाँ हर एक PDO के साथ।
Create (Insert)
<?php
$stmt = $pdo->prepare("INSERT INTO students (name, marks) VALUES (?, ?)");
$stmt->execute(["Aman", 88]);
?>
Read (Select)
<?php
$stmt = $pdo->query("SELECT * FROM students");
foreach ($stmt as $row) {
echo $row["name"] . " - " . $row["marks"] . "<br>";
}
?>
Update
<?php
$stmt = $pdo->prepare("UPDATE students SET marks = ? WHERE id = ?");
$stmt->execute([95, 1]);
?>
Delete
<?php
$stmt = $pdo->prepare("DELETE FROM students WHERE id = ?");
$stmt->execute([5]);
?>
सारांश
- CRUD = Create (INSERT), Read (SELECT), Update, Delete।
- Secure रहने को चारों के लिए prepared statements use करें।