Crud Operations In Php Using OOPs

Crud Operations In Php Using OOPs

Hey, Guys Hope you are all fine, In this tutorial, we will learn crud operations in php using oops. OOP (Object Oriented Programming) is very helpful for reusing codes. In Normal Programming, we use a bunch of codes many times, but you can use the OOP concept, you don’t need to write many times the same code.

Guys, Another Important thing is if you want to learn Any High-Level Programming Language that’s supported by OOP. So, You must learn OOP Concept, because OOP is very important for newbies or beginners. Once you learn the OOP concept, then you will be able to learn Frameworks.

Guys, there are different ways to make a simple application, I’ve used the simple and easiest way to make crud operations in PHP using the OOPs concept. Once you get the idea then you can easily work on another way to make a Crud Operations in Php using OOps.

You May Also Like: Contact Us Form In Php With Email

In Php Have many Frameworks, If you have good knowledge in OOP (Object Oriented Programming), then you can learn Php Frameworks. Without having any knowledge of OOP, you can’t understand the working process of frameworks.

However, I’ve designed the course using the Php OOP (Object Oriented Programming) concept, you will learn step by step How to Design and Also Develop the Application with the name of crud operations in php using oops.

Crud Operations In Php Using OOPs

In the above video series, you can learn step by step How to Perform operations such as crud operations in php using oops. You will learn how to design and also develop complete applications using OOP concept. Below, I’ve mentioned each code used to perform the operations.

Insert Update Delete In Php Using Classes

Before Working on an OOP Project, You must have Basic Knowledge of Php OOP. So, Let’s Talk about How to Insert Update, and Delete in PHP using Classes. First of all, you need to make an index.php file and then write the HTML codes to design the application, I’ve designed the signup form using HTML and Bootstrap. Here is Codes

You May Also Like: Login and Registration System in Php With Email Activation

<?php 
    require_once('./config/dbconfig.php'); 
    $db = new operations();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Crud Operation in Php Using OOP</title>
</head>
<body class="bg-dark"><div class="container">
        <div class="row">
            <div class="col-lg-6 m-auto">
                <div class="card mt-5">
                    <div class="card-header">
                        <h2> Signup Form </h2>
                    </div>
                        <?php $db->Store_Record(); ?>
                        <div class="card-body">
                            <form method="POST">
                                <input type="text" name="First" placeholder=" First Name" class="form-control mb-2" required>
                                <input type="text" name="Last" placeholder=" Last Name" class="form-control mb-2" required>
                                <input type="text" name="UserName" placeholder=" User Name" class="form-control mb-2" required>
                                <input type="Email" name="UserEmail" placeholder=" User Name" class="form-control mb-2" required>
                        </div>
                    <div class="card-footer">
                            <button class="btn btn-success" name="btn_save"> Save </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

In Index.Php Code have a Designing of the Form and Also Php Function that helps you to store the record inside the Database. The need can copy the complete code and paste it into your project index.php File

But you must have a Bootstrap CSS File without a Bootstrap CSS File, You can’t see a signup form professional on the web. Inside the Project, I’ve used Bootstrap Classes to Design it.

Let’s Talk About Php OOP Classes, So, First of all, you should make a folder with the name of the config. Once you made the config folder, then you need to make a dbconfig.php file. Inside the File, you need to make dbconfig class.

Inside the Class you need to make a connection with the database, Here is the code of dbconfig.php File.

<?php 
    session_start();
    require_once('./config/operations.php');

class dbconfig
    {
        public $connection;
        public function __construct()
        {
            $this->db_connect();
        }

        public function db_connect()
        {
            $this->connection = mysqli_connect('localhost','root','','crud');
            if(mysqli_connect_error())
            {
                die(" Connect Failed ");
            }
        }
        public function check($a)
        {
            $return = mysqli_real_escape_string($this->connection,$a);
            return $return;
        }
    }
?>

Inside dbconfig have a database connection also another function with the name of the check. the check function which is used to check the special characters on input text fields.

Finally, You need to Make an operations.php File and Also Class inside the class you need to enter functions. Inside the class is a function which used to store the record inside the database. So, You need to copy that and paste it.

<?php 

require_once('./config/dbconfig.php');
    $db = new dbconfig();
    class operations extends dbconfig
    {
        // Insert Record in the Database
        public function Store_Record()
        {
            global $db;
            if(isset($_POST['btn_save']))
            {
                $FirstName = $db->check($_POST['First']);
                $LastName = $db->check($_POST['Last']);
                $UserName = $db->check($_POST['UserName']);
                $UserEmail = $db->check($_POST['UserEmail']);
                if($this->insert_record($FirstName,$LastName,$UserName,$UserEmail))
                {
                    echo '<div class="alert alert-success"> Your Record Has Been Saved :) </div>';
                }
                else
                {
                    echo '<div class="alert alert-danger"> Failed </div>';
                }
            }
        }

      // Insert Record in the Database Using Query    
        function insert_record($a,$b,$c,$d)
        {
            global $db;
            $query = "insert into employees (FirstName,LastName, UserName,Email) values('$a','$b','$c','$d')";
            $result = mysqli_query($db->connection,$query);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
       // View Database Record
        public function view_record()
        {
            global $db;
            $query = "select * from employees";
            $result = mysqli_query($db->connection,$query);
            return $result;
        }

        // Get Particular Record
        public function get_record($id)
        {
            global $db;
            $sql = "select * from employees where ID='$id'";
            $data = mysqli_query($db->connection,$sql);
            return $data;
        }

     }
?>
// Update Record
        public function update()
        {
            global $db;

if(isset($_POST['btn_update']))
            {
                $ID = $_POST['ID'];
                $FirstName = $db->check($_POST['First']);
                $LastName = $db->check($_POST['Last']);
                $UserName = $db->check($_POST['UserName']);
                $Email = $db->check($_POST['UserEmail']);
                if($this->update_record($ID,$FirstName,$LastName,$UserName,$Email ))
                {
                    $this->set_messsage('<div class="alert alert-success"> Your Record Has Been Updated : )</div>');
                    header("location:view.php");
                }
                else
                {   
                    $this->set_messsage('<div class="alert alert-success"> Something Wrong : )</div>');
                }

            } 

 // Update Function 2
        public function update_record($id,$first,$Last,$User,$Email)
        {
            global $db;
            $sql = "update employees set FirstName='$first', LastName='$Last', UserName='$User', Email='$Email' where ID='$id'";
            $result = mysqli_query($db->connection,$sql);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

Those functions help you to perform the operations such as insert update delete view in Php using the OOP concept, but you need to include another file. First of all, you need to create a view.php file inside you need to use the below code

<?php 

    require_once('./config/dbconfig.php'); 
    $db = new operations();
    $result=$db->view_record();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Crud Operation in Php Using OOP</title>
</head>
<body class="bg-dark">
<div class="container">
        <div class="row">
            <div class="col">
                <div class="card mt-5">
                    <div class="card-header">
                        <h2 class="text-center text-dark"> Employees Record </h2>
                    </div>
                    <div class="card-body">
                        <?php
                              $db->display_message(); 
                              $db->display_message();
                        ?>
                        <table class="table table-bordered">
                            <tr>
                                <td style="width: 10%"> ID </td>
                                <td style="width: 10%"> First Name </td>
                                <td style="width: 20%"> Last Name </td>
                                <td style="width: 20%"> User Name </td>
                                <td style="width: 20%"> User Email </td>
                                <td style="width: 20" colspan="2">Operations</td>
                            </tr>
                            <tr>
                                <?php 
                                    while($data = mysqli_fetch_assoc($result))
                                    {
                                ?>
                                    <td><?php echo $data['ID'] ?></td>
                                    <td><?php echo $data['FirstName'] ?></td>
                                    <td><?php echo $data['LastName'] ?></td>
                                    <td><?php echo $data['UserName'] ?></td>
                                    <td><?php echo $data['Email'] ?></td>
                                    <td><a href="edit.php?U_ID=<?php echo $data['ID'] ?>" class="btn btn-success">Edit</a></td>
                                    <td><a href="del.php?D_ID=<?php echo $data['ID'] ?>" class="btn btn-danger">Del</a></td>
                            </tr>
                            <?php
                                    }
                                ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

You May Also Like: Insert Update Delete in Php With Source Code

In view.php you can see complete database record will appear on the web page and also user will able to perform operations such as Edit and Delete the Record, but you need to make an edit.php page inside the page you need to use this code

<?php 
    require_once('./config/dbconfig.php'); 
    $db = new operations();
    $db->update();
    $id = $_GET['U_ID'];
    $result = $db->get_record($id);
    $data = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Crud Operation in Php Using OOP</title>
</head>
<body class="bg-dark">div class="container">
        <div class="row">
            <div class="col-lg-6 m-auto">
                <div class="card mt-5">
                    <div class="card-header">
                        <h2> Update Record </h2>
                    </div>
                        <?php $db->Store_Record(); ?>
                        <div class="card-body">
                            <form method="POST">
                                <input type="hidden" name="ID" value="<?php echo $data['ID']; ?>">
                                <input type="text" name="First" placeholder=" First Name" class="form-control mb-2" required value="<?php echo $data['FirstName']; ?>">
                                <input type="text" name="Last" placeholder=" Last Name" class="form-control mb-2" required value="<?php echo $data['LastName']; ?>">
                                <input type="text" name="UserName" placeholder=" User Name" class="form-control mb-2" required value="<?php echo $data['UserName']; ?>">
                                <input type="Email" name="UserEmail" placeholder=" User Name" class="form-control mb-2" required value="<?php echo $data['Email']; ?>">
                        </div>
                    <div class="card-footer">
                            <button class="btn btn-success" name="btn_update"> Update </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Once you use to edit.php code, then the user will able to edit the record. Finally, you need to make a del.php file, inside the file you need to enter the below code.

<?php 

require_once('./config/dbconfig.php');
    $db = new operations();

    if(isset($_GET['D_ID']))
    {
        global $db;
        $ID = $_GET['D_ID'];
        if($db->Delete_Record($ID))
        {
            $db->set_messsage('<div class="alert alert-danger">  Your Record Has Been Deleted </div>');
            header("location:view.php");
        }
        else
        {
            $db->set_messsage('<div class="alert alert-danger">  Something Wrong to Delete the Record </div>'); 
        }
    }
?>

Furthermore, I’ve used the session to display the messages, If you want to use session messages. So, You need to copy the below-mentioned code and paste into your operations.php page inside the class.

// Set Session Message
        public function set_messsage($msg)
        {
            if(!empty($msg))
            {
                $_SESSION['Message']=$msg;
            }
            else
            {
                $msg = "";
            }
        }

// Display Session Message
        public function display_message()
        {
            if(isset($_SESSION['Message']))
            {
                echo $_SESSION['Message'];
                unset($_SESSION['Message']);
            }
        }

That’s it, you are a crud application in PHP using OOPs Concept has been completed, and you can use each code on your project, but one thing you need to create a database and also a table, In the database table you need to make 5 columns and you need to modify the code such as change the database name inside Config file and also table name.

Above All about Crud Operation in Php Using OOPs Concept, I hope the video also source code are beneficial to you. If you have to face any problems feel free to contact me.

If you like that, please share the tutorial on social sharing websites and also like it, I am thankful to you, and have a nice day.