Simple Demo of jTable Add,Edit and Delete Using PHP and MySQL

in previous tutorial, we have learn jTable features and integration with php and MySQL.This tutorial is second part of Simple Example of JTable With PHP and MySQLthis tutorials, We will implement add,edit and delete record from mysql database using php.
jTable is very well documented plugin.You can easily do insert,update and delete record functionality using php and mysql.There is inbuilt call back function which will handle your add,edit and delete record request.

I am assuming you have read my previous tutorial of JTable With PHP and MySQL , so i am extending previous tutorial code.

jtable-add-edit-with-php-and-mysql

How to Implement Insert Record in jTable Using PHP and MySQL

Step 1: Added insert callback url into jTable instance.

Select Code
1
createAction: 'response.php?action=create',

Step 2: Added a switch condition into response.php file.

Select Code
1
2
3
case 'create':
    $empCls->insertEmployee();
 break;

Step 3: Added action method in response.php file.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
case 'create':
    $empCls->insertEmployee();
 break;
 
 function insertEmployee() {
        $data = array();
        //print_R($_POST);die;
        $sql = "INSERT INTO `employee` (employee_name, employee_salary, employee_age) VALUES('" . $_POST["employee_name"] . "', '" . $_POST["employee_salary"] . "','" . $_POST["employee_age"] . "');  ";
        
        $result = mysqli_query($this->conn, $sql) or die("error to insert employee data");
        
        //Get last inserted record (to return to jTable)
        $result = mysqli_query($this->conn, "SELECT * FROM employee order by id DESC");
        $row = mysqli_fetch_array($result);

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Record'] = $row;
        echo json_encode($jTableResult);  // send data as json format*/
        
    }

How to Implement Update Record in jTable Using PHP and MySQL

Step 1: Added update callback url into jTable instance.

Select Code
1
updateAction: 'response.php?action=update',

Step 2: Added a switch condition into response.php file.

Select Code
1
2
3
case 'update':
    $empCls->updateEmployee();
 break;

Step 3: Added action method in response.php file

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function updateEmployee() {
        $data = array();
        //print_R($_POST);die;
        $sql = "Update `employee` set employee_name = '" . $_POST["employee_name"] . "', employee_salary='" . $_POST["employee_salary"]."', employee_age='" . $_POST["employee_age"] . "' WHERE id='".$_POST["id"]."'";
        
        $result = mysqli_query($this->conn, $sql) or die("error to update employee data");
        
        //Get last inserted record (to return to jTable)
        //$result = mysqli_query($this->conn, "SELECT * FROM employee order by id DESC");
       // $row = mysqli_fetch_array($result);

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        
        
        echo json_encode($jTableResult);  // send data as json format*/
        
    }

How to Implement Delete Record in jTable Using PHP and MySQL

Step 1: Added delete callback url into jTable instance.

Select Code
1
deleteAction: 'response.php?action=delete'

Step 2: Added a switch condition into response.php file.

Select Code
1
2
3
case 'delete':
    $empCls->deleteEmployee();
 break;

Step 3: Added action method in response.php file

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function deleteEmployee() {
        $data = array();
        //print_R($_POST);die;
        $sql = "delete from `employee` WHERE id='".$_POST["id"]."'";
        
        $result = mysqli_query($this->conn, $sql) or die("error to delete employee data");
        
        //Get last inserted record (to return to jTable)
        //$result = mysqli_query($this->conn, "SELECT * FROM employee order by id DESC");
       // $row = mysqli_fetch_array($result);

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        //$jTableResult['Record'] = $row;
        
        echo json_encode($jTableResult);  // send data as json format*/
        
    }

You can download source code and Demo from below link.

Conclusion :

In previous tutorial, we have learn about basic of jTable jquery grid plugin with PHP and MySQL and display records into jtable grid.This tutroails help to add,edit and delete fucntionality of jTable with php and mysql.