Php CURD Operation Tutorial using MySQLi | FreeProjects1

BHOLU SINGH
0

The Complete Guide to Php CURD Operation Tutorials Using MySQLi for 2022

 CRUD Operations are generally performed on databases, hence, during this PHP CRUD Operations tutorial, you may implement CRUD techniques on MySQL databases with the assistance of PHP.   

 The source code for this project is freely available on our website: FreeProjects1 on the GitHub page and the source code is also given at the end of the page.

Image loading error | FreeProjects1


The Top Ways to Succeed in Php CURD Operation Tutorials Using MySQLi

 The CRUD form includes all the main operations that are performed on a relational database. It stands for:

C = Create

R = Read

U = Update

D = Delete

 MySQLi is a powerful database management system that is used by many developers to store data. In this tutorial, we will be using MySQLi to create tutorials for using CurD in 2022. We will cover topics such as creating databases, importing data, and exporting data. By the end of this tutorial, you will have everything you need to start creating your own CurD tutorials.


Php CURD Operation Tutorials Using MySQLi: A Simple (But Complete) Guide

Project source code


1.1 index file 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CURD Operation in PHP</title>
    <meta name="description" content="CRUD Operations are generally performed on databases, hence, during this PHP CRUD Operations tutorial, you may implement CRUD techniques on MySQL databases with the assistance of PHP">
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <?php
        include 'config.php';

        // $token = rand(11111, 99999);
        // $sql = "INSERT INTO users(name, roll_no, age, token)
        // VALUES('ram', '101', '19', '12345'),
        // ('Radha', '102', '21', '23456'),
        // ('Preeti', '103', '18', '34567')";

        // $rs = mysqli_query($conn, $sql);
        // if($rs){
        //     echo "data inserted successfully";
        // } else{
        //     echo "data not inserted successfully";
        // }

        // now we are going to print all data on screen
        $sql = "SELECT * FROM users";
        $rs = mysqli_query($conn, $sql);

    ?>
    <div class="container">
        <table>
            <tr><td colspan="7"><a href="#">Add New Record</a></td></tr>
            <tr>
                <th>Sn</th>
                <th>Name</th>
                <th>Roll No</th>
                <th>Age</th>
                <th>Token</th>
                <th>Update</th>
                <th>Delete</th>
            </tr>
            <?php
            while($arr = mysqli_fetch_assoc($rs)){
                ?>
                <tr>
                    <td><?php echo $arr['sn'] ?></td>
                    <td><?php echo $arr['name'] ?></td>
                    <td><?php echo $arr['roll_no'] ?></td>
                    <td><?php echo $arr['age'] ?></td>
                    <td><?php echo $arr['token'] ?></td>
                    <td><a href="update.php?token=<?php echo $arr['token'] ?>">Update</a></td>
                    <td><a href="#">Delete</a></td>
                </tr>
                <?php
            }
            ?>
        </table>
    </div>
</body>
</html>


1.2 CSS file 
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}

.container{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}
.container table{
    box-shadow: 0 0 10px green;
    padding: 10px 30px 30px;
}

.container table tr td{
    padding: 12px;
}


.container form{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    padding: 10px 30px 30px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.container form h1{
    margin-bottom: 15px;
    text-align: center;
}
.container form input{
    font-size: 17px;
    padding: 8px 12px;
    border: none;
    outline: none;
    border: 1px solid rgba(0,0,0,0.1);
    margin-bottom: 10px;
    border-radius: 10px;
}
.container form input:hover, .container form input:focus{
    border: 1px solid rgba(0,0,0,0.3);
    transition: 0.3s all;
}
.container form .btns{
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
}
.container form .btns button{
    border: none;
    outline: none;
    padding: 8px 20px;
    font-size: 17px;
    font-weight: 600;
    letter-spacing: 0.3px;
    background-color: tomato;
    color: #fff;
    border-radius: 10px;
    cursor: pointer;
}
.container form .btns button:active{
    transform: scale(0.9);
    transition: 0.3s;
}
.btn{
    border: none;
    outline: none;
    padding: 8px 20px;
    letter-spacing: 0.3px;
    font-size: 17px;
    font-weight: 600;
    text-decoration: none;
    border-radius: 10px;
}
.btn.add_record{
    background-color: rgb(40, 43, 228);
    color: #fff;
}
.btn.delete_record{
    background-color: rgb(212, 25, 25);
    color: #fff;
}
.btn.update_record{
    background-color: rgb(6, 135, 6);
    color: #fff;
}

1.3 configuration file
<?php
    // Connect with database
    // syntax
    // $variable = mysqli_connect("your host name", "your user name", "your database password", "database name");

    $conn = mysqli_connect("localhost", "root", "", "curd_operations");

    // check this query is runs or not
    // if($conn){
    //     echo "connection established";
    // } else{
    //     echo "connection not established";
    // }
?>

1.4 update query page file
<?php
    include 'config.php';

    if(isset($_GET['token'])){
        $token = $_GET['token'];

        $sql = "SELECT * FROM users WHERE token = '$token'";
        $rs = mysqli_query($conn, $sql);
        $arr = mysqli_fetch_assoc($rs);

        ?>
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
            <link rel="shortcut icon" href="favicon.png" type="image/x-icon">
            <link rel="stylesheet" href="style.css">
        </head>
        <body>
            <div class="container">
                <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]) ?>" method="post">
                    <h1>Curd Operation</h1>
                    <input type="text" name="name" value="<?php echo $arr['name'] ?>">
                    <input type="text" name="roll_no" value="<?php echo $arr['roll_no'] ?>">
                    <input type="text" name="age" value="<?php echo $arr['age'] ?>">
                    <input type="hidden" name="token" value="<?php echo $arr['token'] ?>">
                    <div class="btns">
                        <button type="submit" name="update">Update Record</button>
                    </div>
                </form>
            </div>
        </body>
        </html>
        <?php
    }
?>

<?php
    //update nam ke button per click hone ke bad ye query run hogi
    include 'config.php';

    if(isset($_POST['update'])){
        $name = $_POST['name'];
        $roll_no = $_POST['roll_no'];
        $age = $_POST['age'];
        $token = $_POST['token'];

        $sql = "UPDATE users SET name = '$name', roll_no = '$roll_no', age = '$age' WHERE token = '$token'";
        $rs2 = mysqli_query($conn, $sql);
        header('location: index.php');
    }
?>


1.5 delete query page file
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete Query page </title>
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <?php
        include 'config.php';

        if(isset($_GET['token'])){
            $token = $_GET['token'];

            $sql = "DELETE FROM users WHERE token = '$token' ";
            $rs = mysqli_query($conn, $sql);

            header('location: index.php');
        }
    ?>
</body>
</html>

1.6 insert query page file
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Insert Query Page</title>
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]) ?>" method="post">
            <h1>Insert Form</h1>
            <input type="text" name="name" placeholder="Username">
            <input type="text" name="roll_no" placeholder="Enter Roll No">
            <input type="text" name="age" placeholder="Enter Age">
            <div class="btns">
                <button type="submit" name="insert_record">Insert Record</button>
            </div>
        </form>
    </div>

    <?php
        // include config file
        include 'config.php';

        // first we will check button is clicked or not
        // if button is clicked then run this query

        if(isset($_POST['insert_record'])){
            $name = $_POST['name'];
            $roll_no = $_POST['roll_no'];
            $age = $_POST['age'];

            // generate token
            $token = rand(11111, 99999);

            // now, we store our data into database
            $sql = "INSERT INTO users(name, roll_no, age, token) VALUES('$name', '$roll_no', '$age', '$token')";

            $rs = mysqli_query($conn, $sql);

            //if this query runs successfully then we redirect user to index page or home page
            if($rs){
                header('location:index.php');
            } else{
                // if this query failed then run this query
                ?>
                <script>
                    alert('Something went wrong, Try again!');
                </script>
                <?php
            }
           
        }
    ?>
</body>
</html>

☆ Download the source code of this project


Tags:

Post a Comment

0Comments

Please do not enter any spam link in the comment box.

Post a Comment (0)