PHP CRUD Application with MySQL (Create, Read, Update, Delete) – Complete Project
PHP CRUD Application with MySQL (Create, Read, Update, Delete) – Complete Project
In this tutorial, we will build a complete PHP CRUD Application using MySQL. CRUD stands for:
Create (Insert Data)
Read (Fetch Data)
Update (Edit Data)
Delete (Remove Data)
This is one of the most important projects for every PHP developer.
1. Project Structure
crud-app/
│
├── db.php
├── index.php
├── add.php
├── edit.php
├── delete.php
2. Database Setup
Create database:
CREATE DATABASE crud_db;
Create table:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT
);
3. Database Connection (db.php)
<?php
$conn = new mysqli("localhost","root","","crud_db");
if($conn->connect_error){
die("Connection Failed");
}
?>
4. Insert Data (Create - add.php)
<?php
include "db.php";
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$sql = "INSERT INTO users(name,email,age)
VALUES('$name','$email','$age')";
$conn->query($sql);
header("Location: index.php");
}
?>
<form method="POST">
<input type="text" name="name" placeholder="Name" required><br>
<input type="email" name="email" placeholder="Email" required><br>
<input type="number" name="age" placeholder="Age" required><br>
<button type="submit" name="submit">Add User</button>
</form>
5. Read Data (index.php)
<?php
include "db.php";
$result = $conn->query("SELECT * FROM users");
?>
<a href="add.php">Add New User</a>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Actions</th>
</tr>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['email'] ?></td>
<td><?= $row['age'] ?></td>
<td>
<a href="edit.php?id=<?= $row['id'] ?>">Edit</a>
<a href="delete.php?id=<?= $row['id'] ?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
6. Update Data (edit.php)
<?php
include "db.php";
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM users WHERE id=$id");
$user = $result->fetch_assoc();
if(isset($_POST['update'])){
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$conn->query("UPDATE users SET
name='$name',
email='$email',
age='$age'
WHERE id=$id");
header("Location: index.php");
}
?>
<form method="POST">
<input type="text" name="name" value="<?= $user['name'] ?>" required><br>
<input type="email" name="email" value="<?= $user['email'] ?>" required><br>
<input type="number" name="age" value="<?= $user['age'] ?>" required><br>
<button type="submit" name="update">Update</button>
</form>
7. Delete Data (delete.php)
<?php
include "db.php";
$id = $_GET['id'];
$conn->query("DELETE FROM users WHERE id=$id");
header("Location: index.php");
?>
8. Important Improvements (Security Upgrade)
To make this project production-ready:
Use prepared statements (prevent SQL injection)
Validate input data
Escape output using htmlspecialchars()
Add confirmation before delete
Use pagination for large data
9. Features You Can Add (Advanced Version)
Search functionality
Pagination
Login system
API version (CRUD API)
AJAX CRUD without page reload
Admin panel
Role-based access
10. Why This Project is Important
This CRUD project is important because:
It is used in 90% of real PHP applications
It is commonly asked in interviews
It helps you understand database operations
It builds strong backend logic skills
Comments
Post a Comment