02 March 2022

How to update data in php?

 update data  in php?

Guide:
After a database and a table have been created,we can start insert records in it.
after  insert record you need to update record using the form of html and php.
for insert record you need some html code ane insert query.
you need two 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 or update record ?

here i use one condition for check id is null or not.
if id is null than it perform insert query, otherwise it perform update query.

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 method="post">
        <?php
        if (isset($_REQUEST["eid"])) {
            include("config.php");
            $id = $_REQUEST["eid"];
            $sql = "select * from register where id = $id";
            $ins = mysqli_query($conn, $sql);
            $row = mysqli_fetch_row($ins);
        }
        ?>
        <table>
            <tr>
                <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="pass" name="txtpass" value="<?php echo @$row[3]; ?>"></td>
            </tr>
            <tr>
                <td>Mobile No</td>
                <td><input type="text" name="txtmobile" value="<?php echo @$row[4]; ?>"></td>
            </tr>
            <tr>
                <td><input type="submit" name="insert" value="insert"></td>
                <td> <a href="showdata.php">show data</a></td>
            </tr>
        </table>
    </form>
</body>
</html>
<?php
if (isset($_POST["insert"])) {
    include("config.php");

    $name = $_POST["txtname"];
    $email = $_POST["txtemail"];
    $pwd = $_POST["txtpass"];
    $mobile = $_POST["txtmobile"];
    $id = $_REQUEST["eid"];
    if ($id == null) {
        $sql = "INSERT INTO register VALUES('', '$name','$email','$pwd','$mobile')";
    } else {
        $sql = "UPDATE register SET name = '$name',email='$email',password='$pwd',mobileno='$mobile' WHERE id = $id";
    }
    $result = mysqli_query($conn, $sql);
    if ($result) {
        header("location:showdata.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.

here i fetch id from link  name edit

it will redirect to indexpage with id.

showdata.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 register where id = $id";

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

            $row = mysqli_fetch_row($ins);

        }

        ?>

        <table>

            <tr>

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

                </td>

            </tr>

        </table>

        <table border="2px">

            <tr>

                <td>id</td>

                <td>name</td>

                <td>email</td>

                <td>password</td>

                <td>mobileno</td>

                <td>edit</td>

               

            </tr>

            <?php

            include("config.php");

            $sql = "SELECT * FROM register";

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

            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>

                               </tr>

            <?php

            }

            ?>

        </table>

        <table>

            <tr>

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

            </tr>

        </table>

    </form>

</body>

</html>

config.php page

<?php

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

?>

if you still consude under code please watch our video and clear your concept:



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