6. Data Delete

1.Method
obviously have to put this code in fresh.php. where have your database connect.

/* Data Delete */
    
    public function Delete($table,$id){
        $query = mysqli_query($this->connect,"DELETE FROM $table WHERE id=$id");
        if(mysqli_affected_rows($this->connect)>0){
            return true;
        } else{
            return false;
        }
    }

2.tabulize_data.php

<?php
include("fresh.php");
if (isset($_REQUEST['id'])) {
    /* The isset () used to check whether a variable is set or not.
       If a variable is already unset.The isset() return false.
     */
    //super global variable $_REQUEST uses to collect the value of the input field:
    if ($obj->Delete("members", $_REQUEST['id'])) {
        $err_msg = "Successfully Deleted";
    } else {
        $err_msg = "No Data Delete";
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Show Database</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>

    <body>
        <div style="width: 800px; margin: 0 auto">
            <?php
            echo (isset($err_msg)) ? $err_msg : "";
            ?>
            <table class="table table-bordered table-hover" style="text-align: center;">
                <tr>
                    <td>Id</td>
                    <td>Name</td>
                    <td>E-mail</td>
                    <td>Phone</td>
                    <td>Address</td>
                    <td>Action</td>
                </tr>
                <?php
                $team = $obj->getData("members");
                //print_r($team);
                foreach ($team as $single) {
                    extract($single);
                    ?>   
                    <tr>
                        <td><?php echo $id; ?></td>
                        <td><?php echo $name; ?></td>
                        <td><?php echo $email; ?></td>
                        <td><?php echo $phone; ?></td>
                        <td><?php echo $address; ?></td>
                        <td><a href="edit.php?id=<?php echo $id; ?>" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span></a>&nbsp;<a href="tabulize_data.php?id=<?php echo $id; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></a></td>
                    </tr>
                    <?php
                }
                ?>
                <td colspan="6" align="right"><a class="btn btn-info" href="insert.php">Add New Member</a></td>
            </table>
        </div>
    </body>
</html>

Output following screenshot:
Delete

5. Data Edit

1.You have to use two methods one is data fetch and second is data update.
obviously have to put this code in fresh.php. where have your database connect.

/* Data fetch from database */

    public function fetch($table, $id) {
        $query = mysqli_query($this->connect, "SELECT * FROM $table WHERE id=$id");
        if (mysqli_num_rows($query) > 0) {
            return mysqli_fetch_assoc($query);
        } else {
            return false;
        }
    }

    /* Data update */

    public function Update($table, $id, $name, $email, $phone, $address, $edit_id) {
        $query = mysqli_query($this->connect, "UPDATE $table SET id='$id',name='$name',email='$email',phone='$phone',address='$address' WHERE id=$edit_id ");
        if (mysqli_affected_rows($this->connect) > 0) {
            return true;
        } else {
            return false;
        }
    }

2.Create edit.php file and set these following code.

<?php
include_once("fresh.php");
/* data fetch by id */
if (isset($_REQUEST['id'])) {
    //echo "<pre>";
    //print_r($_REQUEST);
    extract($obj->fetch("members", $_REQUEST['id']));
}

if (isset($_REQUEST['edit_id'])) {
    //echo "<pre>";
    //print_r($_REQUEST);
    extract($_REQUEST);
    if ($obj->Update("members", $id, $name, $email, $phone, $address, $edit_id)) {
        header('location:tabulize_data.php');
    } else {
        $err_msg = "Update Fail";
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Show Database</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>

    <body>

        <div style="width: 700px; margin: 0 auto">   
            <?php
            echo(isset($err_msg)) ? $err_msg : "";
            ?>
            <form>
                <table class="table table-bordered table-hover" style="text-align: center;">  
                    <tr>
                        <td colspan="2">Insert Your Information</td>
                    </tr>
                    <tr>
                        <td>Id</td>
                        <td><input name="id"class="form-control" type="text" value="<?php echo @$id; ?>" ></td>
                    </tr>
                    <tr>
                        <td>Name</td>
                        <td><input name="name" class="form-control" type="text" value="<?php echo @$name; ?>" placeholder="Enter Your Name" required="required"></td>
                    </tr>
                    <tr>
                        <td>E-mail</td>
                        <td><input name="email" class="form-control" type="text" value="<?php echo @$email; ?>" placeholder="Enter Your valid Email" required="required"></td>
                    </tr>
                    <tr>
                        <td>Phone</td>
                        <td><input name="phone" class="form-control" type="text" value="<?php echo @$phone; ?>" placeholder="Enter Your Number" required="required"></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><textarea name="address" class="form-control"><?php echo @$address; ?></textarea></td>
                    </tr>  
                    <tr>
                        <td colspan="2" align="center">
                            <!-- need hidden id -->
                            <input type="hidden" name="edit_id" value="<?php echo $id; ?>" />
                            <input type="submit" value="Update" name="submit" class="btn btn-primary">
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

Output following screenshot
edit

4. Data Insert

  1. Method
    obviously have to put this code in fresh.php. where have your database connect.

    public function Insert($table,$id,$name,$email,$phone,$address){
           $query = mysqli_query($this->connect, "INSERT INTO $table SET id='$id', name='$name', email='$email', phone='$phone', address='$address'");
           if(mysqli_affected_rows($this->connect)>0){
               return true;
           }
           else{
               return false;
           }
       }
    

  2. create insert.php file

<?php
include("fresh.php");
if (isset($_REQUEST['submit'])) {
    //print_r($_REQUEST);
    extract($_REQUEST);
    if ($obj->Insert("members", $id, $name, $email, $phone, $address)) {
        header('location:tabulize_data.php'); // Where have your data show
    } else {
        $err_msg = "No Data Insert in Your table";
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Show Database</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>

    <body>
        <div style="width: 700px; margin: 0 auto">
            <!-- ternary operators -->
            <?php
            echo (isset($err_msg)) ? $err_msg : "";
            ?>
            <form>
                <table class="table table-bordered table-hover" style="text-align: center;">  
                    <tr>
                        <td colspan="2">Insert Your Information</td>
                    </tr>
                    <tr>
                        <td>Id</td>
                        <td><input name="id" class="form-control" type="text" ></td>
                    </tr>
                    <tr>
                        <td>Name</td>
                        <td><input name="name" class="form-control" type="text" placeholder="Enter Your Name" required="required"></td>
                    </tr>
                    <tr>
                        <td>E-mail</td>
                        <td><input name="email" class="form-control" type="text" placeholder="Enter Your valid Email" required="required"></td>
                    </tr>
                    <tr>
                        <td>Phone</td>
                        <td><input name="phone" class="form-control" type="text" placeholder="Enter Your Number" required="required"></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><textarea name="address" class="form-control"></textarea></td>
                    </tr>  
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Save" name="submit" class="btn btn-primary"></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

Insert form look like following screenshot:
insert-form

3. Data show in table

1.Method:
obviously have to put this code in fresh.php. where have your database connect.

 public function getData($table) {
        $query = mysqli_query($this->connect, "SELECT * FROM $table");
        if (mysqli_num_rows($query) > 0) {
            return mysqli_fetch_all($query, MYSQLI_ASSOC);
        } else {
            return "No data available in yor table $table";
        }
    }

2.tabulize_data.php

<?php
include("fresh.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Show Database</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>

    <body>
        <div style="width: 700px; margin: 0 auto">
        <table class="table table-bordered table-hover" style="text-align: center;">
            <tr>
                <td>Id</td>
                <td>Name</td>
                <td>E-mail</td>
                <td>Phone</td>
                <td>Address</td>
            </tr>
            <?php
            $team = $obj->getData("members");
            //print_r($team);
            foreach ($team as $single) {
                extract($single);
                ?>   
                <tr>
                    <td><?php echo $id; ?></td>
                    <td><?php echo $name; ?></td>
                    <td><?php echo $email; ?></td>
                    <td><?php echo $phone; ?></td>
                    <td><?php echo $address; ?></td>
                </tr>
                <?php
            }
            ?>
        </table>
        </div>
    </body>
</html>

Show this like following screenshot:
tabulize-data

1. OOP Simple Database Connection

  1. At first you have to create database name, in phpmyadmin.
  2. You have to put a php file in htdocs.
<?php

class fresh{
    public $connect;
    public function __construct($host,$user,$pass,$db) {
        $this->connect = mysqli_connect($host, $user, $pass, $db);
        if(mysqli_connect_errno()){
            die ("your connection fail");
        }
        else{
            echo "Connection success";
        }
    }
    
}

$obj= new fresh("localhost","root","","fresh");