Here’s a basic PHP CRUD (Create, Read, Update, Delete) tutorial that will guide you through building a simple web application using PHP and MySQL.
PHP Create Read Update Delete Tutorial
Setting up the Database
First, you need to set up your database. Create a new MySQL database and table that will hold the data for your application. For this tutorial, let’s assume you have a table named “users” with columns: id (int, auto-increment), name (varchar), email (varchar), and age (int).
Creating the Database Connection
Create a PHP file called “config.php” to establish a connection to your database. Add the following code:
<?php
$host = 'localhost';
$db = 'your_database_name';
$user = 'your_username';
$password = 'your_password';
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
Make sure to replace ‘your_database_name’, ‘your_username’, and ‘your_password’ with your actual database credentials.
Creating the Read Operation
Create a PHP file called “index.php” to display the list of users from the database. Add the following code:
<?php
include 'config.php';
$stmt = $conn->prepare('SELECT * FROM users');
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>CRUD Tutorial</title>
</head>
<body>
<h1>User List</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><?php echo $user['age']; ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
This code retrieves all the users from the database and displays them in an HTML table.
Creating the Create Operation
Create a PHP file called “create.php” to add a new user to the database. Add the following code:
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$stmt = $conn->prepare('INSERT INTO users (name, email, age) VALUES (?, ?, ?)');
$stmt->execute([$name, $email, $age]);
header('Location: index.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create User</title>
</head>
<body>
<h1>Create User</h1>
<form method="POST">
<label>Name:</label>
<input type="text" name="name" required><br>
<label>Email:</label>
<input type="email" name="email" required><br>
<label>Age:</label>
<input type="number" name="age" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This code displays a form to create a new user. When the form is submitted, it inserts the user’s information into the database.
Creating the Update and Delete Operations
To update and delete users, create PHP files called “update.php” and “delete.php” respectively, and include the following code:
For update.php:
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$stmt = $conn->prepare('UPDATE users SET name=?, email=?, age=? WHERE id=?');
$stmt->execute([$name, $email, $age, $id]);
header('Location: index.php');
exit;
}
$id = $_GET['id'];
$stmt = $conn->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>Update User</title>
</head>
<body>
<h1>Update User</h1>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<label>Name:</label>
<input type="text" name="name" value="<?php echo $user['name']; ?>" required><br>
<label>Email:</label>
<input type="email" name="email" value="<?php echo $user['email']; ?>" required><br>
<label>Age:</label>
<input type="number" name="age" value="<?php echo $user['age']; ?>" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
For delete.php:
<?php
include 'config.php';
$id = $_GET['id'];
$stmt = $conn->prepare('DELETE FROM users WHERE id = ?');
$stmt->execute([$id]);
header('Location: index.php');
exit;
?>
These codes allow you to update and delete users from the database.
That’s it! You’ve created a basic PHP CRUD application. You can now navigate to “index.php” to view the list of users, “create.php” to add a new user, and “update.php” and “delete.php” to modify or delete existing users. Remember to replace the database credentials in “config.php” with your own.
Related Articles