PHP Simple CRUD operation in a procedural way

Share your love

This is my first tutorial on this website. Today in this tutorial I will be showing Simple CRUD operations in a PHP procedural way. This tutorial will not use any CSS because It is a simple crud operation so just simply show the crud operation in the PHP programming language. In the futural tutorial, I will use CSS

What is the procedural way?

Procedural is a concept where you write a procedure or function that runs operations on data. This was commonly used in PHP projects. But nowadays programmers use OOP (Object-oriented programming) way in their projects.

But I started my blog at a beginner level so I start with a procedural way. And step by step I will show advanced-level tutorials. so First we need to know, what is CRUD?

What is CRUD Operation?

When you make the main 4 operations is called CRUD, Where

C for Create, R for Read, U for Update, and D for Delete. So In this project, I will show you these four operation by PHP MySql

PHP Simple CRUD operation in a procedural way
PHP Simple CRUD operation in a procedural way

Database code

First I created a MySQL database called ‘codingfordev’. Then I create a table called ‘address’. In the table, I add some column name, email, and addresses. I have given the MySQL table code below

CREATE TABLE `address` (
 `id` int NOT NULL,
 `name` varchar(100) NOT NULL,
 `email` varchar(100) NOT NULL,
 `address` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci

Database connection

After creating the database and table.  Need to create a database connection. I write a few lines of code to create a connection to the MySQL database so I have given the database connection code below

<?php

$server     = "localhost";
$username   = "root";
$password   = "";
$database   = "codingfordev";

$conn = mysqli_connect($server, $username, $password, $database);

if(!$conn){
    die("Connection failed :" . mysqli_connect_error());
}

Create Operation

After creating the database connection I write code for create operation. First of all, I created a file named index.php. In the index.php file, I write code for HTML form. In the HTML form, I take three input boxes to take information from users. The boxes for name email address and addresses. I want to Run a PHP operation from the same file so that before starting the HTML tag I write some PHP code to insert form data into the database. After inserting data into the database, all data show in an HTML table on the index page so I also write a code for showing data in an HTML table. index.php file’s full code is given below

<?php
include "db.php";
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $sql = "INSERT INTO address ( name, email, address ) VALUES ( '$name', '$email', '$address' )";
    $query = mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Code For Dev</title>
</head>
<body>
    <form action="index.php" method="POST">
        <p>
            <label for="name">Name</label>
            <input type="text" name="name" id="name">
        </p>
        <p>
            <label for="email">E-mail</label>
            <input type="text" name="email" id="email">
        </p>
        <p>
            <label for="address">Address</label>
        </p>
        <p>
            <textarea name="address" id="address" cols="30" rows="10"></textarea>
        </p>
        <p>
            <button type="reset">Reset</button>
            <button type="submit" name="submit">Submit</button>
        </p>
    </form>
    <h1>Contact Information</h1>
    <table style="text-align: center;">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Address</th>
                <th>Mange</th>
            </tr>
        </thead>
        <tbody>
        <?php 
            $query = "SELECT * FROM address";
            $result = mysqli_query($conn, $query);
            if(mysqli_num_rows($result)>0){
                foreach( $result as $value) { ?>
                    <tr>
                        <td><?php echo $value['name']; ?></td>
                        <td><?php echo $value['email']; ?></td>
                        <td><?php echo $value['address']; ?></td>
                        <td><a href="update.php?id=<?php echo $value['id']; ?>">Edit</a> || <a onclick="confirm('Are you sure to delete to this information?')" href="delete.php?id=<?php echo $value['id']; ?>">Delete</a></td>
                    </tr>
                    <?php
                }
            }
        ?>
        </tbody>
    </table>
</body>
</html>

Read or View Operation

In the HTML table, every row of data represents every inserted data like name, email, and address. Every row has three links view update and delete. So someone clicks on the ‘View’ link then information of the row view in an additional page. When clicking on the view link then it will pass the row’s ID by URL. PHP code will get ID from URL by PHP GET Method. And using ID PHP runs a query in the database to show specific rows of information and will show all information on the page. Full read operation code is given below

<?php 
include "db.php";

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = $_GET['id'];
    $sql = "SELECT * FROM address where id = $id";
    $result = mysqli_query($conn, $sql);
    $resultNum = mysqli_num_rows($result);
    if ($resultNum != 0) {
    $data = mysqli_fetch_array($result); 
    
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Code For Dev</title>
</head>
<body>
    <h1>Information</h1>
    <h3>Name: <?php echo $data['name']; ?></h3>
    <h3>Email: <?php echo $data['email']; ?></h3>
    <address>Address: <?php echo $data['address']; ?></address>
</body>
</html>
<?php
    }
}

Update Operation

Using an update operation can easily update information from the table. In the table see the update link. To show updates just click on the update link. The form will show previously inserted data. When clicking on the ‘Update’ link then it will pass the row’s ID by URL. PHP code will get ID from URL by PHP GET Method. Then it showed in HTML form. After changing some value of the form field then click on update to update data. After clicking the update button of the form it takes value from updated form fields and runs an update query to update data in the database. The updat code is given below

<?php 
include "db.php";

if(isset($_POST['submit'])){

    $id     = $_POST['id'];
    $name   = $_POST['name'];
    $email  = $_POST['email'];
    $address = $_POST['address'];
    
    $sql = "UPDATE address SET name = '$name', email = '$email', address = '$address' where id= $id"; 

    $query = mysqli_query($conn,$sql);
    if($query){
        header("Location: index.php");
    }
}

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = $_GET['id'];
    $sql = "SELECT * FROM address where id = $id";
    $result = mysqli_query($conn, $sql);
    $resultNum = mysqli_num_rows($result);
    if ($resultNum != 0) {
    $data = mysqli_fetch_array($result); 
    
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Code For Dev</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
        <p>
            <label for="name">Name</label>
            <input type="text" name="name" id="name" value="<?php echo $data['name']; ?>">
        </p>
        <p>
            <label for="email">Email</label>
            <input type="text" name="email" id="email" value="<?php echo $data['email']; ?>">
        </p>
        <p>
            <label for="address">Address</label>
        </p>
        <p>
            <textarea name="address" id="address" cols="30" rows="10"><?php echo $data['address']; ?></textarea>
        </p>
        <p>
            <input type="hidden" name="id" value="<?php echo $data['id']; ?>">
            <button type="submit" name="submit">Submit</button>
        </p>
    </form>
</body>
</html>
<?php
    }
}

Delete Operation

To delete some information from the database just click on the delete button. After clicking on the delete button it shows an alert message to confirm the delete information from the database. When clicking on the ‘Delete’ link then it will show a confirm delete information from the database, then click on the ok of confirm message then pass the row’s ID by URL. PHP code will get the ID from the URL by PHP GET Method and also delete information using delete query by id.  Delete query code given

<?php

include "db.php";


if(isset($_GET['id']) && is_numeric($_GET['id'])){

    $id = $_GET['id'];
    $sql = "SELECT * FROM address where id = $id";
    $result = mysqli_query($conn, $sql);
    $resultNum = mysqli_num_rows($result);
    if ($resultNum != 0) {
    
        $sql = "DELETE from address where id= $id"; 

        echo 'delete';

        $query = mysqli_query($conn,$sql);
        if($query){
            header("Location: index.php");
        }
    }
}

Conclusion

It is it was my first tutorial and there may be some mistakes. So please forgive me if you found some mistakes in this tutorial. If you like this article please follow me on social media profiles. And if you want to get this kind of valuable tutorial in your email please subscribe to the Codingfordev newsletter. Thank you for reading and stay connected

Share your love
Khan Sunny
Khan Sunny

I'm a big fan of the PHP programming language. At the end of 2012, I started writing codes in PHP. From 2013, I started working on Wordpress Theme and Plugins customization as well as building Full Websites with Wordpress. My experience includes developing, managing and executing maintenance projects using PHP raw coding and also with PHP Codeigniter. At present, I am working successfully in Fiverr and Upwork

Articles: 1

Leave a Reply

Your email address will not be published. Required fields are marked *