PHP Crud Operations - Select, Insert, Update, Delete | PHP Tutorial For Beginners | Simplilearn
Table of Contents
Introduction
This tutorial will guide you through the fundamental CRUD (Create, Read, Update, Delete) operations in PHP, specifically tailored for beginners. By the end of this guide, you will understand how to interact with a MySQL database using PHP, leveraging tools like XAMPP and Visual Studio Code.
Step 1: Setting Up Your Environment
To start working with PHP and MySQL, you need to set up your development environment.
-
Install XAMPP:
- Download XAMPP from the official website.
- Follow the installation instructions for your operating system.
-
Start Apache and MySQL:
- Open the XAMPP Control Panel.
- Click on
Start
next to Apache and MySQL.
-
Create a Database:
- Access phpMyAdmin by navigating to
http://localhost/phpmyadmin
. - Click on
Databases
and create a new database (e.g.,crud_example
).
- Access phpMyAdmin by navigating to
Step 2: Creating the Database Table
Now, you need to create a table to perform CRUD operations.
-
Create a Table:
- In phpMyAdmin, select your database.
- Click on
SQL
and run the following command to create a table:
CREATE TABLE users ( id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL );
Step 3: Connecting to the Database
You will need to connect your PHP application to the MySQL database.
-
Create a Connection File:
- In your project folder, create a file named
db.php
and add the following code:
<?php $servername = "localhost"; $username = "root"; // default XAMPP username $password = ""; // default XAMPP password $dbname = "crud_example"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
- In your project folder, create a file named
Step 4: Creating the CRUD Operations
You will implement the four basic CRUD operations.
Create Operation
-
Insert Data:
- Create a file named
create.php
and add the following code:
<?php include 'db.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST['name']; $email = $_POST['email']; $sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } } ?> <form method="post" action=""> Name: <input type="text" name="name" required> Email: <input type="email" name="email" required> <input type="submit" value="Submit"> </form>
- Create a file named
Read Operation
-
Fetch Data:
- Create a file named
read.php
to display the data:
<?php include 'db.php'; $sql = "SELECT id, name, email FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>"; } } else { echo "0 results"; } ?>
- Create a file named
Update Operation
-
Update Data:
- Create a file named
update.php
and add the following code:
<?php include 'db.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; $sql = "UPDATE users SET name='$name', email='$email' WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } } ?> <form method="post" action=""> ID: <input type="text" name="id" required> Name: <input type="text" name="name" required> Email: <input type="email" name="email" required> <input type="submit" value="Update"> </form>
- Create a file named
Delete Operation
-
Delete Data:
- Create a file named
delete.php
:
<?php include 'db.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = $_POST['id']; $sql = "DELETE FROM users WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } } ?> <form method="post" action=""> ID: <input type="text" name="id" required> <input type="submit" value="Delete"> </form>
- Create a file named
Conclusion
In this tutorial, you learned how to set up a PHP environment and perform basic CRUD operations with a MySQL database. You created a database, established a connection, and implemented the functionality to create, read, update, and delete records.
Next, you can explore more advanced topics such as prepared statements for security, using frameworks like Laravel, or creating RESTful APIs. Happy coding!