How to Fetch data by ID in Textbox from database in PHP MySQL

By Super Admin | May 31, 2021 | PHP
Share :

https://www.fundaofwebit.com/post/how-to-fetch-data-by-id-in-textbox-from-database-in-php-mysql

Fetch data by ID in Textbox from database in PHP MySQL


Hey Developers,

In this post, you will be learning how to fetch data by id into Html input box or textbox from database in php mysql, which means getting the data from database in input box.

So, First we will create a HTML Form to search the data by ID in php on the click of submit button and then process the code using php mysql and then finally display the data in HTML Input box.

We are using Bootstrap 5 version to design the user interface.

So, Lets get started guys: 

Step 1: Create a mysql table in database named student and insert /add few records in it:

CREATE TABLE `student` (
    `id` int(11NOT NULL AUTO_INCREMENT,
    `stud_name` varchar(191NOT NULL,
    `stud_class` varchar(100NOT NULL,
    `stud_phone` varchar(100NOT NULL,
    PRIMARY KEY (`id`)
);

Step 2: Create a file named index.php and paste the below code as follows:

<!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>Funda of Web IT</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-7">

                <div class="card mt-5">
                    <div class="card-header text-center">
                        <h4>How to Fetch Data by ID in Textbox using PHP MySQL</h4>
                    </div>
                    <div class="card-body">

                        <form action="" method="GET">
                            <div class="row">
                                <div class="col-md-8">
                                    <input type="text" name="stud_id" value="<?php if(isset($_GET['stud_id'])){echo $_GET['stud_id'];} ?>" class="form-control">
                                </div>
                                <div class="col-md-4">
                                    <button type="submit" class="btn btn-primary">Search</button>
                                </div>
                            </div>
                        </form>

                        <div class="row">
                            <div class="col-md-12">
                                <hr>
                                <?php 
                                    $con = mysqli_connect("localhost","root","","database_name");

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

                                        $query = "SELECT * FROM student WHERE id='$stud_id";
                                        $query_run = mysqli_query($con$query);

                                        if(mysqli_num_rows($query_run) > 0)
                                        {
                                            foreach($query_run as $row)
                                            {
                                                ?>
                                                <div class="form-group mb-3">
                                                    <label for="">Name</label>
                                                    <input type="text" value="<?= $row['stud_name']; ?>" class="form-control">
                                                </div>
                                                <div class="form-group mb-3">
                                                    <label for="">Class</label>
                                                    <input type="text" value="<?= $row['stud_class']; ?>" class="form-control">
                                                </div>
                                                <div class="form-group mb-3">
                                                    <label for="">Phone No</label>
                                                    <input type="text" value="<?= $row['stud_phone']; ?>" class="form-control">
                                                </div>
                                                <?php
                                            }
                                        }
                                        else
                                        {
                                            echo "No Record Found";
                                        }
                                    }
                                   
                                ?>

                            </div>
                        </div>

                    </div>
                </div>

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

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Now, we can check the output


Thanks for reading...

https://www.fundaofwebit.com/post/how-to-fetch-data-by-id-in-textbox-from-database-in-php-mysql

Share this blog on social platforms