Update and DELETE record from mysql database with CodeIgniter in PHP

 <?php

defined("BASEPATH") or exit("No direct script access allowed");

class Registration extends CI_Controller
{
    //If we want to call any library in All Controller then we have to call that library in
    //autoload.php and if we want to call any library in all functions of any one particular Controller class
    //then we have to call that library in constructor of that particular Controller class.
    public function __construct()
    {
        parent::__construct();
        $this->load->library("form_validation");
        $this->load->model("Registration_model");
    }

    public function index()
    {
        echo "call registration function";
    }

    public function add()
    {
        if ($this->uri->segment(3)) {
            $data["pageType"] = "Update";
            $editId = $this->uri->segment(3);
            $data["editInfo"] = $this->Registration_model->getUserInfoById($editId);
        } else {
            $data["pageType"] = "Add";
        }


        if ($this->input->post()) {
            $this->form_validation->set_error_delimiters("<div class='error'>", "</div>");
            $this->form_validation->set_rules("userName", "Username", "trim|required|callback_checkName");
            $this->form_validation->set_rules("emailId", "Email", "trim|required|valid_email");
            $this->form_validation->set_rules("phoneNo", "Phone Number", "required");
            $this->form_validation->set_rules("password", "Password", "required");
            $this->form_validation->set_rules("confirm_password", "Confirm Password", "required|matches[password]");

            $this->form_validation->set_message("required", "{field} must be filled");
            $this->form_validation->set_message("valid_email", "Email address must be a valid email");
            if ($this->form_validation->run() !== FALSE) {

                $record["name"] = $this->input->post("userName");
                $record["email"] = $this->input->post("emailId");
                $record["phone_no"] = $this->input->post("phoneNo");
                $record["password"] = $this->input->post("password");

                if ($data["pageType"] == "Update") {
                    $response = $this->Registration_model->updateRecord($record, $editId);
                } else {
                    $response = $this->Registration_model->addRecord($record);
                }

                if ($response) {
                    $message = array("message" =>  $data["pageType"] . " Successfully", "class" => "alert alert-success");
                    $this->session->set_flashdata("myMsg", $message);
                } else {
                    $message = array("message" => "Something went wrong", "class" => "alert alert-danger");
                    $this->session->set_flashdata("msMsg", $message);
                }
                redirect("/Registration/table");
            }
        }
        $title = $data["pageType"] . "Registration";
        $this->load->view("common/header", array("title" => $title));
        $this->load->view("reg_form", $data);
        $this->load->view("common/footer");
    }

    public function table()
    {
        $data["userList"] = $this->Registration_model->getRecord();
        $this->load->view("common/header");
        $this->load->view("reg_table", $data);
        $this->load->view("common/footer");
    }

    public function delete($id)
    {
        $response = $this->Registration_model->deleteRecord($id);
        if ($response) {
            $message = array("message" => "Deleted Successfully", "class" => "alert alert-success");
            $this->session->set_flashdata("myMsg", $message);
        } else {
            $message = array("message" => "Something went wrong", "class" => "alert alert-success");
            $this->session->set_flashdata("myMsg", $message);
        }
        redirect("/Registration/table");
    }

    public function checkName($str)
    {
        if ($str == "test") {
            $this->form_validation->set_message("checkName", "The {field} field can not be the word 'test'");
            return FALSE;
        } else {
            return TRUE;
        }
    }
}




HEADER FILE
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .error {
            color: red
        }
    </style>
    <title><?php echo (isset($title)) ? $title : "Registration"; ?></title>
</head>

<body>


FOOTER FILE
</body>

</html>




REGISTRATION TABLE
<div class="container-fluid">
    <div class="jumbotron text-center">
    </div>

    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <button class="btn btn-primary"><a href="<?php echo base_url("Registration/add/"); ?>" style="color:white">Add</a></button>
            <?php
            if ($this->session->flashdata("myMsg")) {
                $message = $this->session->flashdata("myMsg");
            ?>
                <div class="<?php echo $message["class"]; ?>"><?php echo $message["message"]; ?> </div>
            <?php
            }
            ?>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone No</th>
                        <th>Password</th>
                        <th>Created At</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($userList as $row) { ?>
                        <tr>
                            <td><?php echo $row->id; ?></td>
                            <td><?php echo $row->name; ?></td>
                            <td><?php echo $row->email ?></td>
                            <td><?php echo $row->phone_no; ?></td>
                            <td><?php echo $row->password ?></td>
                            <td><?php echo $row->created_at; ?></td>
                            <td>
                                <button class="btn btn-primary"><a href="<?php echo base_url("Registration/edit/") . $row->id; ?>" style="color:white">Edit</a></button>
                                <button class="btn btn-danger"><a href="<?php echo base_url("Registration/delete/") . $row->id; ?>" style="color:white">Delete</a></button>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>






REGISTRATION FORM
<div class="container-fluid">
    <div class="jumbotron text-center">
    </div>
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">

            <form method="post" action="<?php if (isset($editInfo)) {
                                            echo base_url("Registration/edit/") . $editInfo->id;
                                        } else {
                                            echo base_url("Registration/add");
                                        } ?>">

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Name</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter name" name="userName" id="userName" value="<?php if (isset($editInfo)) {
                                                                                                                                    echo $editInfo->name;
                                                                                                                                } else {
                                                                                                                                    echo set_value("userName");
                                                                                                                                } ?>">
                    </div>
                    <?php echo form_error("userName"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="Email">Email</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter Email" name="emailId" id="emailId" value="<?php if (isset($editInfo)) {
                                                                                                                                    echo $editInfo->email;
                                                                                                                                } else {
                                                                                                                                    echo set_value("emailId");
                                                                                                                                } ?>">
                    </div>
                    <?php echo form_error("emailId"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Phone No</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" placeholder="Enter Phone No" name="phoneNo" id="phoneNo" value="<?php if (isset($editInfo)) {
                                                                                                                                    echo $editInfo->phone_no;
                                                                                                                                } else {
                                                                                                                                    echo set_value("phoneNo");
                                                                                                                                } ?>">
                    </div>
                    <?php echo form_error("phoneNo"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Password</label>
                    <div class="col-sm-4">
                        <input type="password" class="form-control" placeholder="Enter Password" name="password" id="password" value="<?php if (isset($editInfo)) {
                                                                                                                                            echo $editInfo->password;
                                                                                                                                        } else {
                                                                                                                                            echo set_value("password");
                                                                                                                                        } ?>">
                    </div>
                    <?php echo form_error("password"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-1"></div>
                    <label class="col-sm-3" for="name">Confirm Password</label>
                    <div class="col-sm-4">
                        <input type="password" class="form-control" placeholder="Confirm Password" name="confirm_password" id="confirm_password" value="<?php if (isset($editInfo)) {
                                                                                                                                                            echo $editInfo->password;
                                                                                                                                                        } else {
                                                                                                                                                            echo set_value("confirm_password");
                                                                                                                                                        } ?>">
                    </div>
                    <?php echo form_error("confirm_password"); ?>
                </div>

                <div class="form-group row">
                    <div class="col-sm-4"></div>
                    <div class="col-sm-4">
                        <button type="submit" class="btn btn-primary"><?php echo $pageType; ?></button>
                        <button class="btn btn-danger"><a href="<?php echo base_url("Registration/table/"); ?>" style="color:white">Back</a></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>






REGISTRATION MODEL
<?php
defined("BASEPATH") or exit("No direct script access allowed");

class Registration_model extends CI_Model
{
    //INSERT data in mysql database
    public function addRecord($data)
    {
        $this->db->insert("student", $data);
        return $this->db->insert_id();
    }

    //SELECT record from mysql database
    public function getRecord()
    {
        $this->db->select("*");
        $this->db->from("student");
        $query = $this->db->get()->result();
        return $query;
    }

    //SELECT record by Id
    public function getUserInfoById($id)
    {
        $this->db->select("*");
        $this->db->from("student");
        $this->db->where("id", $id);
        $query = $this->db->get()->row();   //Here, row() is used for single record and result() is used for multiple records
        return $query;
    }

    //UPDATE record from mysql database
    public function updateRecord($data, $id)
    {
        $this->db->where("id", $id);
        $this->db->update("student", $data);
        return true;
    }

    //DELETE record from mysql database
    public function deleteRecord($id)
    {
        $this->db->where("id", $id);
        $this->db->delete("student");
        return true;
    }
}





















Comments