WT LAB 23010088
Assignment No: 7
Title: PHP
Objective:
To design and develop web applications using front end technologies and
ebackend databases
Problem Statement:
Build a dynamic web application using PHP and MySQL.
a. Create database tables in MySQL and create connection with PHP.
b. Create the add, update, delete and retrieve functions in the PHP web app
interacting with MySQL database.
Theory:
Introduction to PHP and MySQL
PHP (Hypertext Preprocessor) is a powerful server-side scripting language widely
used for web development. It allows developers to create dynamic web pages
that interact with a MySQL database to store and retrieve information.
MySQL is a relational database management system (RDBMS) that enables
structured data storage and efficient retrieval using SQL (Structured Query
Language). When combined, PHP and MySQL offer a scalable, efficient, and
flexible solution for building web applications that require database interaction.
Advantages of Using PHP with MySQL
1. Open-Source & Free: Both PHP and MySQL are open-source technologies,
making them cost-effective.
2. Cross-Platform Compatibility: PHP and MySQL work seamlessly on various
operating systems like Windows, Linux, and macOS.
3. High Performance: MySQL efficiently handles large amounts of data, and
PHP processes requests quickly.
4. Ease of Use: PHP has a simple syntax, and MySQL is based on structured
query language (SQL), making them easy to learn and implement.
5. Secure Data Handling: PHP supports built-in security features like data
encryption and SQL injection prevention mechanisms.
6. Scalability: Suitable for small websites as well as large-scale web
applications.
Step-by-Step Process of Building a PHP-MySQL Web Application
1. Database Setup and Table Creation
WT LAB 23010088
A database is created in MySQL to store application data.
Tables are structured with appropriate fields and data types.
Primary keys and foreign key constraints are defined to maintain
data integrity.
2. Establishing Database Connection in PHP
PHP connects to MySQL using the MySQLi (MySQL Improved) or PDO
(PHP Data Objects) extension.
Database connection parameters like host, username, password, and
database name are configured.
A connection check is implemented to handle connection failures
gracefully.
3. Implementing CRUD Functionalities in PHP
Create (Adding Data)
An HTML form collects user input, such as name, email, or product
details.
The input data is validated and sanitized before insertion into the MySQL
database.
SQL’s INSERT INTO statement is used to store the data securely.
Read (Fetching and Displaying Data)
The application retrieves records from the database to display on web
pages.
SQL’s SELECT statement fetches data based on criteria (e.g., show all
users or a specific user).
Data is formatted into tables, lists, or card layouts for a user-friendly
interface.
Update (Editing and Modifying Data)
Users can modify existing data using an edit form.
The old data is retrieved and displayed in form fields for editing.
Upon submission, SQL’s UPDATE statement modifies the record in the
database.
Delete (Removing Data)
WT LAB 23010088
A delete button allows users to remove specific records from the
database.
A confirmation prompt prevents accidental deletions.
SQL’s DELETE statement permanently removes the selected record.
Program:
Index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User List</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
<h2 class="text-center">User List</h2>
<!-- Add New Record Button -->
<a href="add.php" class="btn btn-primary mb-3">Add New Record</a>
<table class="table table-bordered bg-white shadow">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
WT LAB 23010088
</thead>
<tbody>
<?php
include 'db.php';
$result = $conn->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
<td>{$row['phone']}</td>
<td>
<a href='edit.php?id={$row['id']}' class='btn btn-warning
btn-sm'>✏ Edit</a>
<a href='delete.php?id={$row['id']}' class='btn btn-danger
btn-sm'>🗑 Delete</a>
</td>
</tr>";
}
?>
</tbody>
</table>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.j
s"></script>
</body>
</html>
Add.php
<?php
WT LAB 23010088
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$conn->query("INSERT INTO users (name, email, phone) VALUES ('$name',
'$email', '$phone')");
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Record</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
<div class="card shadow-lg p-4">
<h2 class="text-center text-success">➕ Add New Record</h2>
<form method="POST">
<div class="mb-3">
<label class="form-label">Name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="mb-3">
<label class="form-label">Email:</label>
WT LAB 23010088
<input type="email" class="form-control" name="email" required>
</div>
<div class="mb-3">
<label class="form-label">Phone:</label>
<input type="tel" class="form-control" name="phone" required>
</div>
<button type="submit" class="btn btn-success w-100">✔ Save
Record</button>
<a href="index.php" class="btn btn-secondary w-100 mt-2">⬅
Back</a>
</form>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.j
s"></script>
</body>
</html>
Delete.php
<?php
include 'db.php';
$id = $_GET['id'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn->query("DELETE FROM users WHERE id=$id");
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
WT LAB 23010088
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete User</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
<div class="card shadow-lg p-4">
<h2 class="text-center text-danger">Delete User</h2>
<p class="text-center">Are you sure you want to delete this user?</p>
<form method="POST" class="text-center">
<button type="submit" class="btn btn-danger w-100">Yes,
Delete</button>
<a href="index.php" class="btn btn-secondary w-100
mt-2">Cancel</a>
</form>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.j
s"></script>
</body>
</html>
Edit.php
<?php
include 'db.php';
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM users WHERE id = $id");
WT LAB 23010088
$row = $result->fetch_assoc();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$conn->query("UPDATE users SET name='$name', email='$email',
phone='$phone' WHERE id=$id");
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit User</title>
<link
href="https://cdn.jsdelivr.net/npm/
[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
<div class="card shadow-lg p-4">
<h2 class="text-center text-primary">Edit User</h2>
<form method="POST">
<div class="mb-3">
<label class="form-label">Name:</label>
<input type="text" class="form-control" name="name" value="<?=
$row['name'] ?>" required>
</div>
WT LAB 23010088
<div class="mb-3">
<label class="form-label">Email:</label>
<input type="email" class="form-control" name="email" value="<?=
$row['email'] ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Phone:</label>
<input type="tel" class="form-control" name="phone" value="<?=
$row['phone'] ?>" required>
</div>
<button type="submit" class="btn btn-success w-100">Save
Changes</button>
<a href="index.php" class="btn btn-secondary w-100 mt-2">Back</a>
</form>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.j
s"></script>
</body>
</html>
Db.php
<?php
$servername = "localhost";
$username = "root"; // Change this as per your MySQL user
$password = "Krutika@007"; // Change this as per your MySQL password
$database = "my_database";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
WT LAB 23010088
}
?>
Style.css
body {
background: linear-gradient(to right, #ff7e5f, #feb47b);
font-family: 'Poppins', sans-serif;
}
.table {
background: white;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.btn {
border-radius: 5px;
}
Output:
WT LAB 23010088
WT LAB 23010088
WT LAB 23010088
Conclusion:
A PHP-MySQL dynamic web application efficiently manages CRUD operations for data
manipulation. By implementing secure database connections, form validation, and
session handling, developers can create scalable and robust web applications. The
integration of frontend and backend technologies ensures an interactive and user-
friendly experience.