04 March 2022

Crud Operation in php?

    how to perform Crud Operation in php?

Guide:

After a database and a table have been created,we can start insert records in it.
after all you need to insert record in data base using the form of html and php.
for insert record you need some html code ane insert query.
you need three pages

table Query: 

CREATE TABLE `register` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(30) NOT NULL,

  `email` varchar(30) NOT NULL,

  `password` varchar(30) NOT NULL,

  `mobileno` bigint(10) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4

how to insert record ?

remove if condition  and update query which i mention in this page

like>if (isset($_POST["insert"])) {

    include("config.php");

    $name = $_POST["txtname"];

    $email = $_POST["txtemail"];

    $pwd = $_POST["txtpass"];

    $mobile = $_POST["txtmobile"];

    $sql = "INSERT INTO register VALUES('', '$name','$email','$pwd','$mobile')";

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

    if ($result) {

        header("location:showdata.php");

    }

}

for insert record and you can also add another button for update and another isset function for update,

but here i did it on one button

when id is null it happen insert operation,

and when my id is not null it happens update operation.



config.php page

<?php
// for connection
$conn = mysqli_connect("localhost","root","","demo1");
if(!$conn){
    echo "connection error";
}
?>

index.php page

<!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>

</head>

<body>

    <form action="" method="post">

        <?php 

    if(isset($_REQUEST["eid"])){

        include("config.php");

        $id = $_REQUEST["eid"];

        $sql = "SELECT * FROM registration WHERE id = $id";

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

        $row = mysqli_fetch_row($ins);

        }

        ?>

    <table>

        <tr>

            <!-- name is on number 1 position -->

            <td>Name</td>

            <td><input type="text" name="txtname" value="<?php echo @$row[1]; ?>"></td>

        </tr>

        <tr>

            <td>Email</td>

            <td><input type="email" name="txtemail" value="<?php echo @$row[2]; ?>"></td>

        </tr>

        <tr>

            <td>Password :</td>

            <td><input type="password" name="txtpwd" value="<?php echo @$row[3]; ?>"></td>

        </tr>

        <tr>

            <td>Mobile No :</td>

            <td><input type="text" name="txtmo" value="<?php echo @$row[4]; ?>"></td>

        </tr>

        <tr>

            <td><input type="submit" name="submit" value="submit"></td>

            <td><a href="display.php">display record</a></td>

        </tr>

    </table>

    </form>

</body>

</html>

<?php

if(isset($_POST["submit"])){

    include("config.php");

    $name = $_POST["txtname"];

    $email = $_POST["txtemail"];

    $pwd = $_POST["txtpwd"];

    $mono = $_POST["txtmo"];

    $id = $_REQUEST["eid"];

    if($id == null){

    $sql = "INSERT INTO registration VALUES('','$name','$email','$pwd','$mono')";

}else{

    $sql = "UPDATE registration SET name = '$name',email = '$email',password = '$pwd',mobileno = '$mono' WHERE id = $id";

}

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

    if($result){

        header("location:display.php");

    }

}

?>


how to display data in our page?

The second operation is  used to display or read the data that is already available in the database.



display.php page

<!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>

</head>

<body>

    <form action="" method="post">

    <table border="2px solid black">

        <tr>

            <td><a href="index.php">home</a></td>

        </tr>

        <tr>

            <td>ID</td>

            <td>NAME</td>

            <td>EMAIL</td>

            <td>PASSWORD</td>

            <td>MOBILENO</td>

            <td>EDIT</td>

            <td>DELETE</td>

        </tr>

        <?php

    include("config.php");

    $sql = "SELECT * FROM registration";

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

    if(mysqli_num_rows($ins) > 0){

    while($row = mysqli_fetch_row($ins)){

    ?>

    <tr>

        <td><?php echo $row[0]; ?></td>

        <td><?php echo $row[1]; ?></td>

        <td><?php echo $row[2]; ?></td>

        <td><?php echo $row[3]; ?></td>

        <td><?php echo $row[4]; ?></td>

        <td><a href="index.php?eid=<?php echo $row[0]; ?>">edit</a></td>

        <td><a href="display.php?eid=<?php echo $row[0]; ?>">delete</a></td>

    </tr>

<?php

    }

}else{

    echo "no data found";

}

        ?>

    </table>

    </form>

</body>

</html>

<?php

if(isset($_REQUEST["eid"])){

    include("config.php");

    $id = $_REQUEST["eid"];

    $sql = "DELETE FROM registration WHERE id = $id";

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

    if($ins){

    header("location:display.php");

    }

}

?>

if you still dont understand look our video 




No comments:

Post a Comment

If You have any doubt, or want to know about something, please let me know

how to create custom plugin deactivate hook and display all post in wordpress?

 how to create custom plugin deactivate hook and display all post in wordpress? make new folder in plugin after whatever you want your folde...