Search

PHP CRUD Tutorial

4 min read
1 views

Here's a basic PHP CRUD (Create, Read, Update, Delete) tutorial that will guide you through building a simple web application using PHP Create Read Update Delete Tutorial

Prompt
<?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.

Prompt
<?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.

Prompt
<?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.

For update.php:

Prompt
<?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:

Prompt
<?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.

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!