File Download

/* download.php */

<?php

if (isset($_GET['file_name'])) {
    $filename = $_GET['file_name'];
} else {
    $filename = NULL;
}

$err = "Sorry, the file you are requesting is unavailable";

if (!$filename) {
    echo $err;
} else {
    $path = 'uploads/' . $filename;
    if (file_exists($path) && is_readable($path)) {
        $size = filesize($path);
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . $size);
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');

        $file = @fopen($path, 'rb');
        if ($file) {
            fpassthru($file);
            exit;
        } else {
            echo $err;
        }
    }
}
?>

File upload & Delete

<?php
$conn = mysqli_connect("localhost", "root", "", "fresh");

/* This is for delete file */
/*
if (isset($_REQUEST['file_name'])) {
    $filename = $_REQUEST['file_name'];
    $query = mysqli_query($conn, "DELETE FROM user WHERE profile_image='$filename'");
    if (mysqli_affected_rows($conn) > 0) {
        @ unlink("uploads/$filename");
        echo "Your file succefully delete";
    } else {
        echo "Your file not delete";
    }
}
 * */



/* file upload from this here */
if (isset($_FILES['filefield'])) {

    $name = $_REQUEST['name'];
    $file = $_FILES['filefield'];
    //echo "<pre>";
    //print_r($file);

    $upload_directory = "uploads/";

    $allowed_extensions = array("gif", "jpg", "jpeg", "mp3", "mp4", "tiff", "bmp", "doc", "docx", "ppt", "pptx", "txt", "pdf");
    $ext = substr($file['name'], strrpos($file['name'], '.') + 1);
    if (!in_array($ext, $allowed_extensions)) {
        die('only "gif", "jpg", "jpeg", "mp3", "tiff", "bmp", "doc", "docx", "ppt", "pptx", "txt", "pdf" files allowed to upload');
    }

    $max_file_size = 10485760;
    if ($file['size'] >= $max_file_size) {
        echo "Only the file size less than $max_file_size mb allowed";
    }

    $path = md5(microtime()) . '.' . $ext;

    if (move_uploaded_file($file['tmp_name'], $upload_directory . $path)) {

        // This query file name insert in to the database
        $query = mysqli_query($conn, "INSERT INTO user SET name='$name', profile_image='$path' ");

        //if (move_uploaded_file($file['tmp_name'], $upload_directory . $file['name'])) {
        echo "Your file successfully uploaded";
    } else {
        echo "The file can't moved";
    }
}
?>
/* file upload finished */

<form action="" method="post" enctype="multipart/form-data">
    <label>Name
        <input id="name" type="input" name="name" />
    </label>

    <label>Upload File
        <input id="filefield" type="file" name="filefield" />
    </label>

    <label>
        <input id="Upload" type="submit" name="Upload" value="Submit" />
    </label>



    <!-- This part is file name fetch from database and show up table -->

    <table width="700" border="1">
        <tr>
            <td>Name</td>
            <td>File Name</td>
            <td>Image</td>
        </tr>

        <?php
        /*
        $query = mysqli_query($conn, "SELECT name, profile_image FROM user");
        $hold = mysqli_fetch_all($query, MYSQLI_ASSOC);
        //echo "<pre>";
        //print_r($show);
        foreach ($hold as $data) {
            extract($data);
            ?>     
            <tr>
                <td><?php echo $name; ?></td>
                <td><?php echo $profile_image; ?></td>
                <!--<td><img src="uploads/<?php //echo $profile_image;?> "/></td>-->
                <td>
                    <?php
                    $file_ext = substr($profile_image, strrpos($profile_image, '.') + 1);
                    if ($file_ext == 'jpg') {
                        ?>
                        <img src = "uploads/<?php echo $profile_image; ?>" width = "100" />
                        <?php
                    }

                    if ($file_ext == 'pdf') {
                        ?>
                        <a href="uploads/<?php echo $profile_image; ?>"><img src = "uploads/pdf.jpg" width = "100" /></a>
                        <?php
                    }
                    if ($file_ext == 'docx') {
                        ?>
                        <a href="uploads/<?php echo $profile_image; ?>"><img src = "uploads/docx.jpg" width = "100" /></a>
                        <?php
                    }
                    ?>
                    <a href="download.php?file_name=<?php echo $profile_image; ?>"><input type="button" value="Download" name="download"></a>
                    <a href="upload.php?file_name=<?php echo $profile_image; ?>"><input type="button" value="Delete" name="delete" /></a>     
                </td>

            </tr>
            <?php
        }
        
        */
        ?>
    </table>

</form>

Normal OOP Registration

1.Method Create

<?php

class Crud {

	public $conn;

	public function __construct() {
		$this->conn = mysqli_connect( "localhost", "root", "", "fresh" );
		if ( mysqli_connect() ) {
			//echo "Connect Successfully";
		} else {
			die( "connection error" );
		}
		session_start();
	}

	/* registration insert */

	public function regiInsert( $table, $firstname, $lastname, $username, $email, $password, $phone, $gender, $address ) {
		$query = mysqli_query( $this->conn, "INSERT INTO $table SET firstname='$firstname', lastname='$lastname', username='$username', email='$email', password='$password', phone='$phone', gender='$gender', address='$address' " );
		if ( mysqli_affected_rows( $this->conn ) > 0 ) {
			return true;
		} else {
			return false;
		}
	}


}

$obj = new Crud();

2.registration.php

<?php
include_once('../crud.php');
if (isset($_REQUEST['save'])) {
    //echo "<pre>";
    //print_r($_REQUEST);
    extract($_REQUEST);
    if ($obj->regiInsert("registration", $firstname, $lastname, $username, $email, sha1($password), $phone, $gender, $address)) {
        $err_msg = "Successfully You Registered, Please Sign in";
    } else {
        $err_msg = "Your registration not include";
    }
}
if(isset($_SESSION['username'])){
	header('location:home.php');
}
?>



<!doctype html>
<html>
    <head>
        <title>Registration Form</title>
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"/>

        <!-- BootstrapValidator CSS -->
        <link rel="stylesheet" href="../bootstrap/css/bootstrapValidator.min.css"/>

        <!-- jQuery and Bootstrap JS -->
        <script type="text/javascript" src="../bootstrap/js/jquery-1.11.3.min.js"></script>
        <script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>

        <!-- BootstrapValidator JS -->
        <script type="text/javascript" src="../bootstrap/js/bootstrapValidator.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div style="border: 1px solid #ddd; padding: 0 0 15px;" class="col-md-8 col-md-offset-2">
                    <div class="page-header">
                        <h2 style="text-align: center; font-family: initial;">Sign up</h2>
                        <p style="text-align:center; color: #4D73E4;">
                            <?php
                            echo isset($err_msg) ? $err_msg : "";
                            ?>
                        </p>
                    </div>

                    <form action="registration.php" id="registrationForm" method="post" class="form-horizontal">

                        <div class="form-group">
                            <label class="col-xs-3 control-label">Full name</label>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" name="firstname" placeholder="First name" />
                            </div>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" name="lastname" placeholder="Last name" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Username</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" name="username" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Email address</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" name="email" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Password</label>
                            <div class="col-sm-5">
                                <input type="password" class="form-control" name="password" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-xs-3 control-label">Phone number</label>
                            <div class="col-xs-5">
                                <input type="text" class="form-control" name="phone" placeholder="Phone Number" />
                            </div>
                        </div>	

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Gender</label>
                            <div class="col-sm-5">
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="gender" value="male" /> Male
                                    </label>
                                </div>
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="gender" value="female" /> Female
                                    </label>
                                </div>
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="gender" value="other" /> Other
                                    </label>
                                </div>
                            </div>
                        </div>
                        <!-- 
                        <div class="form-group">
                             <label class="col-sm-3 control-label">Date of birth</label>
                             <div class="col-sm-5">
                                 <input type="date" class="form-control" name="date"  placeholder="YYYY/MM/DD" />
                             </div>
                         </div>-->

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Address</label>
                            <div class="col-sm-5">
                                <textarea name="address" class="form-control" rows="5" cols="25"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-9 col-sm-offset-3">
                                <!-- Do NOT use name="submit" or id="submit" for the Submit button -->
                                <button type="submit" name="save" value="submit" class="btn btn-default">Sign up</button>
                                <a class="btn btn-default" href="http://127.0.0.1/ibcs/fresh/template/admin/">Sign In</a>
                            </div>
                            
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <script>
            $(document).ready(function () {
                $('#registrationForm').bootstrapValidator({
                    // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        firstname: {
                            row: '.col-xs-4',
                            validators: {
                                notEmpty: {
                                    message: 'The first name is required'
                                }
                            }
                        },
                        lastname: {
                            row: '.col-xs-4',
                            validators: {
                                notEmpty: {
                                    message: 'The last name is required'
                                }
                            }
                        },
                        username: {
                            message: 'The username is not valid',
                            validators: {
                                notEmpty: {
                                    message: 'The username is required and cannot be empty'
                                },
                                stringLength: {
                                    min: 6,
                                    max: 30,
                                    message: 'The username must be more than 6 and less than 30 characters long'
                                },
                                regexp: {
                                    regexp: /^[a-zA-Z0-9]+$/,
                                    message: 'The username can only consist of alphabetical and number'
                                },
                                different: {
                                    field: 'password',
                                    message: 'The username and password cannot be the same as each other'
                                }
                            }
                        },
                        email: {
                            validators: {
                                notEmpty: {
                                    message: 'The email address is required and cannot be empty'
                                },
                                emailAddress: {
                                    message: 'The email address is not a valid'
                                }
                            }
                        },
                        password: {
                            validators: {
                                notEmpty: {
                                    message: 'The password is required and cannot be empty'
                                },
                                different: {
                                    field: 'username',
                                    message: 'The password cannot be the same as username'
                                },
                                stringLength: {
                                    min: 8,
                                    message: 'The password must have at least 8 characters'
                                }
                            }
                        },
                        phone: {
                            row: '.col-xs-5',
                            validators: {
                                notEmpty: {
                                    message: 'The cell phone number is required'
                                }
                            }
                        },
                        birthday: {
                            validators: {
                                notEmpty: {
                                    message: 'The date of birth is required'
                                },
                                date: {
                                    format: 'YYYY/MM/DD',
                                    message: 'The date of birth is not valid'
                                }
                            }
                        },
                        gender: {
                            validators: {
                                notEmpty: {
                                    message: 'The gender is required'
                                }
                            }
                        }
                        ,
                        address: {
                            validators: {
                                notEmpty: {
                                    message: 'Please input your valid address'
                                }
                            }
                        }
                    }
                });
            });
        </script>
    </body>
</html>

3.looking like this
registration

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");