How to Fetch and Update Data From Database in PHP

Fetch, display and update data from database using form in php. In this post, i will show you from scratch on how to get/fetch data from MySQL database using form in PHP. And as well as display this data on html form.

In this example post, first of all, i will create database, and database connecting file. Then will fetch data and display in html form in PHP. After that, i will update form data from MySQL database in PHP.

Let’s start to tutorial for fetch, display and update data from mysql database in php using form.

Fetch, Display And Update Data From Database in PHP Using Id

You can follow below given simple steps for how to fetch, display and update data from database using form in php:

Step 1 – Create Project Folder

In step 1, navigate to local web server directory. And inside directory, create one folder. And you can name this folder anything.

For example, i will use xampp server so, i willl navigate to C/xampp/htdocs/.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Run SQL query to Create Table

In step 2, open your browser and type http://localhost/phpmyadmin. Then create database and table by running the following sql query:


CREATE TABLE `customers` (
PRIMARY KEY (`custId`)

INSERT INTO `customers` (`custId`, `fname`, `lname`, `email`, `created`) VALUES (NULL, 'Tiago', 'Sam', '[email protected]', NULL), (NULL, 'Anil', 'Kumar', '[email protected]', NULL);

Step 3 – Connecting Database

In step 3, create a php file that named mydbCon.php. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon.php file and add the below given code into your file:


    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");

          die('Could not Connect MySql Server:' .mysql_error());

Step 4 – Fetch Data From Database in PHP using Id

In step 4, you need to create a php file that named fetch-data-by-id.php. Because this file code will fetch data from MySQL database table using id in PHP.

Note that, in next step, i will include this file on index.php.

So add the following code on fetch-data-by-id.php file:


include 'mydbCon.php';

$sql = "SELECT * FROM customers WHERE custId='" . $_GET["custId"] . "'"; // Fetch data from the table customers using id


$customer = mysqli_fetch_assoc($result);


Step 5 – Display Data In Html Form PHP

In step 5, you need create a php file that named index.php. Because, on index.php file will display data from database in form.

And when you click submit button, this form submit to update.php file.

Note that, you need to include fetch-data-by-id.php file on index.php file. Don’t worry i have already include this file on index.php file. Because this fetch-data-by-id.php file code will get single row data from database table in php using id.

So, you can add this code in your index.php file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>How to Fetch and Update Data from Database in PHP</title>
    <link rel="stylesheet" href="">

<div class="container mt-2">

  <div class="page-header">
      <h2>Fetch and Update Data From Database in PHP Using Form</h2>

    <div class="row">
        <div class="col-md-12">
            <?php include 'fetch-data-by-id.php'; ?>
            <form action="update.php" method="POST">

              <input type="hidden" name="custId" value="<?php echo $_GET["custId"]; ?>" class="form-control">

              <div class="form-group">
                <label for="exampleInputEmail1">First Name</label>
                <input type="text" name="fname" class="form-control" value="<?php echo $customer['fname']; ?>">

              <div class="form-group">
                <label for="exampleInputEmail1">Last Name</label>
                <input type="text" name="lname" class="form-control" value="<?php echo $customer['lname']; ?>">

              <div class="form-group">
                <label for="exampleInputEmail1">Email address</label>
                <input type="email" name="email" class="form-control" value="<?php echo $customer['email']; ?>">

              <button type="submit" class="btn btn-primary" value="submit">Submit</button>



Step 6 – Update Data From MySQL Database

In this step 6, you need to create update.php file, So that the data of the form can be updated in the database table.

Now, add the following code into update.php file:



include 'mydbCon.php';

$sql = "UPDATE customers set custId='" . $_POST['custId'] . "', fname='" . $_POST['fname'] . "', lname='" . $_POST['lname'] . "', email='" . $_POST['email'] . "' WHERE custId='" . $_POST['custId'] . "'"; // update form data from the database


echo "Form Data has been updated Successfully in PHP";

  echo "Form Data has not been updated in PHP";


Step 7 – Open Browser And Run This Project

Finally, you need to open your browser and type http://localhost/demo/?custId=1 this run project on local machine.

How to fetch, display and update data from database using form in php will be looks like shown in below image:

Thanks for reading this post.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *