PHP & MySQL Backend Basics: Connecting with PDO & Secure CRUD Operations
Learn how dynamic websites work. Connect PHP to MySQL using PDO and perform secure Create, Read, Update, Delete operations.
Introduction: How Dynamic Websites Work
Static websites show the same content to everyone. Dynamic websites generate content based on data—users, posts, products. The typical stack is:
- Front-end: HTML, CSS, JavaScript (browser)
- Back-end: PHP (server)
- Database: MySQL (storage)
When a user requests a page, PHP runs on the server, queries MySQL, and returns HTML to the browser.
1. Setting Up
Install PHP and MySQL via XAMPP, MAMP, or a Linux stack. Create a database:
CREATE DATABASE eduvalle_demo;
USE eduvalle_demo;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
2. Connecting to MySQL with PDO
PDO (PHP Data Objects) is the recommended way to connect securely and supports prepared statements.
<?php
$host = "localhost";
$db = "eduvalle_demo";
$user = "root";
$pass = "";
$charset = "utf8mb4";
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
echo "Connected successfully!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
3. Create: Inserting Data
<?php
$sql = "INSERT INTO students (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
":name" => "Amina Rahman",
":email" => "amina@example.com",
]);
echo "Student added!";
?>
Prepared statements prevent SQL injection—never concatenate user input into queries.
4. Read: Fetching Data
<?php
$stmt = $pdo->query("SELECT id, name, email FROM students");
$students = $stmt->fetchAll();
foreach ($students as $s) {
echo htmlspecialchars($s["name"]) . " - " . htmlspecialchars($s["email"]) . "<br>";
}
?>
Always escape output with htmlspecialchars() to prevent XSS attacks.
5. Update: Modifying Data
<?php
$sql = "UPDATE students SET name = :name WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
":name" => "Amina Khan",
":id" => 1,
]);
echo "Updated!";
?>
6. Delete: Removing Data
<?php
$sql = "DELETE FROM students WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([":id" => 1]);
echo "Deleted!";
?>
7. A Simple HTML Form
<form method="post" action="add_student.php">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<button type="submit">Add Student</button>
</form>
<?php
// add_student.php
require "db.php";
$name = $_POST["name"] ?? "";
$email = $_POST["email"] ?? "";
if ($name && $email) {
$stmt = $pdo->prepare("INSERT INTO students (name, email) VALUES (?, ?)");
$stmt->execute([$name, $email]);
echo "Saved!";
}
?>
8. Security Best Practices
- Use prepared statements — never concatenate queries.
- Escape output with
htmlspecialchars(). - Hash passwords with
password_hash(). - Validate input on the server, not just the browser.
- Use HTTPS in production.
Conclusion
You now have a working PHP + MySQL backend with secure CRUD operations. This is the foundation of frameworks like Laravel. Practice by building a small student management app.
Ready to Launch Your Website Online?
Once you finish building your project locally, you need fast and reliable hosting to put it online. Students and developers can get an automatic 20% discount (code: AR10-5) on their first order at Hostnin NVMe Web Hosting (plans start from ৳149/mo with free SSL and bKash/Nagad support), or view full details in our Eduvalle Student Shop.
Whether you are launching an online store, agency, or portfolio without coding from scratch, explore finished, mobile-ready websites with free full setup and 100% code ownership at Nowvelle.
Eduvalle-এর একাডেমিক টিম: HSC ও ভর্তি পরীক্ষার প্রস্তুতির জন্য যাচাইকৃত নোট ও কুইজ।