How to edit and update data using oops in php mysql

How to edit and update data using oops in php mysql


In this post, you will be learning how to edit and update data using oops in php, where we edit data from database in html form and updated the data into database using oops concept in php mysql.

We are using bootstrap v5 to design the html form/user interface.

Let's get started with edit update data in php mysql using oops.

Step 1: Add a edit button in your fetched data from database (eg: student-view.php).

When we click on below edit button, a parameter is sent in the url, with the name id (eg: student-edit.php?id=1).

<td>
    <a href="student-edit.php?id=<?=$row['id'];?>" class="btn btn-success">Edit</a>
</td>


Step 2: Create a student-edit.php and paste the below code:

When you click the edit button, student-edit.php opens with the url student-edit.php?id=1 .

We will be fetching the record using the id parameter, (in the url), in the input box.

Note : Include your database connection and oops class created as StudentController.

<?php
include('dbconn.php');
include_once('StudentController.php');
?>

<!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>PHP OOPS - Edit & Update Data into database in php mysql using oops</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>

    <div class="container-fluid px-4">
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4>Student Edit</h4>
                    </div>
                    <div class="card-body">
                        <?php
                        if(isset($_GET['id']))
                        {
                            $student_id = mysqli_real_escape_string($db->conn, $_GET['id']);
                            $student = new StudentController;
                            $result = $student->edit($student_id);

                            if($result)
                            {
                                ?>
                                <form action="student-code.php" method="POST">
                                    <input type="hidden" name="student_id" value="<?=$result['id']?>">

                                    <div class="mb-3">
                                        <label for="">Full Name</label>
                                        <input type="text" name="fullname" value="<?=$result['fullname']?>" required class="form-control" />
                                    </div>
                                    <div class="mb-3">
                                        <label for="">Email ID</label>
                                        <input type="text" name="email" value="<?=$result['email']?>" required class="form-control" />
                                    </div>
                                    <div class="mb-3">
                                        <label for="">Course</label>
                                        <input type="text" name="course" value="<?=$result['course']?>" required class="form-control" />
                                    </div>
                                    <div class="mb-3">
                                        <label for="">Phone No</label>
                                        <input type="text" name="phone" value="<?=$result['phone']?>" required class="form-control" />
                                    </div>
                                    <div class="mb-3">
                                        <button type="submit" name="update_student" class="btn btn-primary">Update Student</button>
                                    </div>
                                </form>
                           
                            <?php
                            }
                            else
                            {
                                echo "<h4>No Record Found</h4>";
                            }
                        }
                        else
                        {
                            echo "<h4>Something Went Wront</h4>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>


Step 3: Create a StudentController.php and paste the below code:

In the edit() method, we are getting the data and displaying in the above student-edit form.

In the update() method, we are going to submit the form in step 2 and then update data into database in this update() function.

<?php

class StudentController
{
    public function __construct()
    {
        $db = new DatabaseConnection;
        $this->conn = $db->conn;
    }

    public function edit($id)
    {
        $student_id = mysqli_real_escape_string($this->conn, $id);
        $studentQuery = "SELECT * FROM students WHERE id='$student_id' LIMIT 1";
        $result = $this->conn->query($studentQuery);
        if($result->num_rows == 1){
            $data = $result->fetch_assoc();
            return $data;
        }else{
            return false;
        }
    }

    public function update($inputData, $id)
    {
        $student_id = mysqli_real_escape_string($this->conn, $id);
        $fullname = $inputData['fullname'];
        $email = $inputData['email'];
        $phone = $inputData['phone'];
        $course = $inputData['course'];

        $studentQuery = "UPDATE students SET fullname='$fullname', email='$email', phone='$phone', course='$course' WHERE id='$student_id' LIMIT 1";
        $result = $this->conn->query($studentQuery);
        if($result){
            return true;
        }else{
            return false;
        }
    }
}
?>


Step 4: Create a student-code.php file and paste the below code:

<?php
include('dbconn.php');
include_once('StudentController.php');

if(isset($_POST['update_student']))
{
    $id = mysqli_real_escape_string($db->conn,$_POST['student_id']);
    $inputData = [
        'fullname' => mysqli_real_escape_string($db->conn,$_POST['fullname']),
        'email' => mysqli_real_escape_string($db->conn,$_POST['email']),
        'phone' => mysqli_real_escape_string($db->conn,$_POST['phone']),
        'course' => mysqli_real_escape_string($db->conn,$_POST['course']),
    ];
    $student = new StudentController;
    $result = $student->update($inputData, $id);

    if($result)
{
        $_SESSION['message'] = "Student Added Successfully";
        header("Location: student-view.php");
        exit(0);
    }
else
{
        $_SESSION['message'] = "Student Not Added";
        header("Location: student-view.php");
        exit(0);
    }
}

?>


Thanks for reading.