Absolutely! Hereβs a complete Contact Book PHP project using MySQL with HTML and CSS. This project is perfect for beginners and covers full CRUD operations (Create, Read, Update, Delete).
Feature | Description |
---|---|
Add Contact | Create a new contact with name, email, phone |
View Contacts | Display a list of contacts |
Edit Contact | Update a contactβs info |
Delete Contact | Remove a contact from the list |
contact-book/
β
βββ db.php # DB connection
βββ index.php # Contact list
βββ add.php # Add contact form
βββ edit.php # Edit contact
βββ delete.php # Delete logic
βββ styles.css # CSS file
βββ README.md # (optional) instructions
CREATE DATABASE contact_book;
USE contact_book;
CREATE TABLE contacts (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(20)
);
db.php
β Database Connection<?php
$host = 'localhost';
$user = 'root';
$password = ''; // Change if needed
$database = 'contact_book';
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
index.php
β Show All Contacts<?php include 'db.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Contact Book</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Contact Book</h1>
<a class="button" href="add.php">+ Add New Contact</a>
<table>
<tr>
<th>Name</th><th>Email</th><th>Phone</th><th>Actions</th>
</tr>
<?php
$result = $conn->query("SELECT * FROM contacts");
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>".htmlspecialchars($row['name'])."</td>
<td>".htmlspecialchars($row['email'])."</td>
<td>".htmlspecialchars($row['phone'])."</td>
<td>
<a href='edit.php?id={$row['id']}'>Edit</a> |
<a href='delete.php?id={$row['id']}' onclick='return confirm(\"Delete this contact?\");'>Delete</a>
</td>
</tr>";
}
?>
</table>
</div>
</body>
</html>
add.php
β Add New Contact<?php include 'db.php';
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$stmt = $conn->prepare("INSERT INTO contacts (name, email, phone) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $phone);
$stmt->execute();
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Contact</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2>Add New Contact</h2>
<form method="post">
<label>Name:</label><br>
<input type="text" name="name" required><br><br>
<label>Email:</label><br>
<input type="email" name="email"><br><br>
<label>Phone:</label><br>
<input type="text" name="phone"><br><br>
<button class="button" type="submit">Add Contact</button>
</form>
</div>
</body>
</html>
edit.php
β Edit Contact<?php
include 'db.php';
$id = $_GET['id'] ?? 0;
if (!$id) header("Location: index.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$stmt = $conn->prepare("UPDATE contacts SET name=?, email=?, phone=? WHERE id=?");
$stmt->bind_param("sssi", $name, $email, $phone, $id);
$stmt->execute();
header("Location: index.php");
exit();
} else {
$stmt = $conn->prepare("SELECT * FROM contacts WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$contact = $result->fetch_assoc();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Contact</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2>Edit Contact</h2>
<form method="post">
<label>Name:</label><br>
<input type="text" name="name" value="<?= htmlspecialchars($contact['name']) ?>" required><br><br>
<label>Email:</label><br>
<input type="email" name="email" value="<?= htmlspecialchars($contact['email']) ?>"><br><br>
<label>Phone:</label><br>
<input type="text" name="phone" value="<?= htmlspecialchars($contact['phone']) ?>"><br><br>
<button class="button" type="submit">Update Contact</button>
</form>
</div>
</body>
</html>
delete.php
β Delete Contact<?php
include 'db.php';
$id = $_GET['id'] ?? 0;
if ($id) {
$stmt = $conn->prepare("DELETE FROM contacts WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
}
header("Location: index.php");
exit();
?>
styles.css
β Basic CSSbody {
font-family: Arial, sans-serif;
background: #f9fafb;
padding: 20px;
}
.container {
max-width: 700px;
margin: auto;
background: white;
padding: 20px;
border-radius: 8px;
}
h1, h2 {
color: #1e293b;
}
.button {
background-color: #0284c7;
color: white;
padding: 10px 15px;
text-decoration: none;
border-radius: 5px;
display: inline-block;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}
th, td {
padding: 10px;
border: 1px solid #ccc;
text-align: left;
}
contact-book
in htdocs/
.http://localhost/contact-book/index.php