PHP CRUD Operation with MySQL

PHP CRUD Operation with MySQL, PHP CRUD Operation

Demo Available Here



In this PHP CRUD Tutorial, we'll use Bootstrap and MySQL to develop a fully functional PHP CRUD application. Create, Read, Update, and Delete (PHP CRUD) is a simple way to remember all of the components required for a fully dynamic website application. We'll use INSERT, SELECT, UPDATE, and DELETE statements in MySQL, which equate to CRUD capability. 

 

7 Step for PHP CRUD Operation with MySQL

 

1. Create Database.

 

"CREATE database crud";

 

Also, See

Create Database phpMyAdmin

Create MySQL Database XAMPP Shell

CREATE DATABASE using PHP

 

2. Create a Table.

 

"CREATE table user (id int (11) AUTO_INCREMENT, username varchar (255), useremail varchar (255), userphone varchar (12), primary key (id)) ";

 

Also, See

Create Database Table phpMyAdmin

Create Database Table XAMPP Shell

CREATE DATABASE Table using PHP

 

3. Database Connection (db_con.php).

 

$dbc = mysqli_connect("localhost","root","", "crud") or die ("could not connect to mysql"); 

 

4. Insert/Create Record (create.php).

 

< ?php
require_once 'db_con.php';
if (isset($_POST['add_new'])) {
    
    $username = $_POST['username'];
    $useremail = $_POST['useremail'];
    $userphone = $_POST['userphone'];

    $sql = "INSERT into user (username, useremail, userphone) values ('$username', '$useremail', '$userphone')";
    if(mysqli_query($dbc, $sql)){

        echo "Data Inserted";
        echo("< script >location.href = 'index.php' ;< /script >");
    }else{

        echo "Error";
    }
}
? >

 

< !DOCTYPE html >
< html >
< head >
    < meta charset="utf-8" >
    < meta name="viewport" content="width=device-width, initial-scale=1" >
    < title >PHP CRUD Operation< /title >
    < !-- CSS only -- >
< link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >
< !-- JavaScript Bundle with Popper -- >
< script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">

< /head >
< body >

    < section style="padding-top:100px;" >
        < div class="container" >
            < div class="row" >
                < div class="col-md-8" >
                    < h1 >PHP CRUD Operation< /h1 >
                    < form method="post" action="" >
                        < div class="form-group" >
                            < input type="text" name="username" placeholder="UserName" class="form-control" >
                        < /div >< br >
                        < div class="form-group" >
                            < input type="email" name="useremail" placeholder="UserEmail" class="form-control" >
                        < /div >< br >
                        < div class="form-group" >
                            < input type="text" name="userphone" placeholder="UserPhone" class="form-control" >
                        < /div >< br >
                        < div class="form-group" >
                            < button name="add_new" type="submit" class="btn btn-primary" >Add New< /button >
                        < /div >
                        
                    < /form >
                < /div >
            < /div >
        < /div >
    < /section >

< /body >
< /html >

 

PHP CRUD Create Record

 

5. Fetch/Read Record (index.php).

 

< ?php
require_once 'db_con.php';
    $sql = "SELECT * from user";

    $run = mysqli_query($dbc, $sql);

    $i=1;
? >

 

< !DOCTYPE html >
< html >
< head >
    < meta charset="utf-8" >
    < meta name="viewport" content="width=device-width, initial-scale=1" >
    < title >PHP CRUD Operation< /title >
    < !-- CSS only -- >
< link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >
< !-- JavaScript Bundle with Popper -- >
< script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" >< /script >

< /head >
< body >

    < section style="padding-top:100px;" >
        < div class="container" >
            < div class="row" >
                < h1 >PHP CRUD Operation< /h1 >
                < div class="col-md-12" >
                    < table class="table table-striped" >
                        < thead >
                            < th >#< /th >
                            < th >Username< /th >
                            < th >Useremail< /th >
                            < th >Userphone< /th >
                            < th >Action< /th >
                        < /thead >
                        < tbody >
                            < ?php 

                                while ($row = mysqli_fetch_assoc($run)) {
                                    
                                    $id = $row['id'];
                                    $username = $row['username'];
                                    $useremail = $row['useremail'];
                                    $userphone = $row['userphone'];
                                

                             ?>
                            < tr >
                                < td >< ?=$i;? >< /td >
                                < td >< ?=$username? >< /td >
                                < td >< ?=$useremail? >< /td >
                                < td >< ?=$userphone? >< /td >
                                < td >
                                    < a href="edit.php?id=< ?=$id? >" class="btn btn-success" >Update< /a >
                                    < a href="delete.php?id=< ?=$id? >" class="btn btn-danger" >Delete< /a >
                                < /td >
                            < /tr >
                            < ?php $i++; } ? >
                        < /tbody >
                    < /table >< br >

                    < a href="create.php" class="btn btn-primary btn-lg" >Add New Record< /a >

                < /div >
            < /div >
        < /div >
    < /section >        

< /body >
< /html >

 

PHP CRUD Fetch Record

 

6. Update Record (update.php).

 

< ?php 
require_once 'db_con.php';
$get_id = '';
if ($_GET['id']) {
$get_id = $_GET['id'];
$sql = "SELECT * from user WHERE id = $get_id";
$run = mysqli_query($dbc, $sql);
$row = mysqli_fetch_assoc($run);
}if (isset($_POST['save'])) {
    
    $username = $_POST['username'];
    $useremail = $_POST['useremail'];
    $userphone = $_POST['userphone'];

    $sql = "UPDATE user set username = '$username', useremail= '$useremail', userphone = '$userphone' where id = $get_id";
    if(mysqli_query($dbc, $sql)){

        echo "Data Updated";
       echo("< script >location.href = 'index.php' ;< /script >");
    }else{

        echo "Error";
    }
}
? >

 

< !DOCTYPE html >
< html >
< head >
    < meta charset="utf-8" >
    < meta name="viewport" content="width=device-width, initial-scale=1" >
    < title>PHP CRUD Operation
    < !-- CSS only -- >
< link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >
< !-- JavaScript Bundle with Popper -- >
< script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">

< /head >
< body >

    < section style="padding-top:100px;" >
        < div class="container" >
            < div class="row" >
                < div class="col-md-8" >
                    < h1 >PHP CRUD Operation< /h1 >
                    < form method="post" action="" >
                        < div class="form-group" > 
                            < input type="text" name="username" placeholder="UserName" class="form-control" value="< ?=$row['username']? >" >
                        < /div >< br >
                        < div class="form-group" >
                            < input type="email" name="useremail" placeholder="UserEmail" class="form-control" value="< ?=$row['useremail']? >" >
                        < /div >< br >
                        < div class="form-group" >
                            < input type="text" name="userphone" placeholder="UserPhone" class="form-control" value="< ?=$row['userphone']? >" >
                        < /div >< br >
                        < div class="form-group" >
                            < button name="save" type="submit" class="btn btn-success" >Save Changes< /button >
                        < /div >
                        
                    < /form >
                < /div >
            < /div >
        < /div >
    < /section >

< /body >
< /html >

 

PHP CRUD Update Record

 

7. Delete Record (create.php).

 

< ?php
require_once 'db_con.php';
$get_id = '';
if ($_GET['id']) {
    
    $get_id = $_GET['id'];

    $sql = "DELETE from user WHERE id = $get_id";

    if(mysqli_query($dbc, $sql)){

        echo "Data Deleted";
        echo("< script >location.href = 'index.php' ;< /script >");
    }else{

        echo "Error";
    }
}
? >

 

Also, Read

MySQL SELECT Statement

MySQL UPDATE Statement

 

 

Download Source Code

Latest Updates

You May Also Like


PHP Projects

PHP Data Filter between Two Dates Project with Source Code

PHP Data Filter between Two Dates - You will learn how to filter or find data between two dates in PHP/MySQL in this project.....

JavaScript Projects

JavaScript Weight Converter Project with Source Code

JavaScript Weight Converter Download free with source code - Simple Weight Converter App using HTML & JavaScript that converts kilograms to other measurements.....

PHP Projects

PHP Multiple Image Upload Project with Source Code

Multiple Image Upload using PHP and MySQL download free with source code - 1. Create Database and Table. 2. Create HTML Form. 3. PHP Code for Upload an Image and PHP Code for Display an Image.....

JavaScript Projects

JavaScript Stopwatch Project with Source Code

JavaScript Stopwatch Download with source code - 1. Html code for timer and buttons. 2. Javascript code for set timer, hr min, sec and start, stop and reset.....

PHP Projects

PHP Image Upload Project with Source Code

Image Upload using PHP and MYSQL download free project with source code - 1. Create Database and Table. 2. Create HTML Form. 3. PHP Code for Upload an Image and PHP Code for Display an Image.....