---
description: https://laratutorials.com/how-to-fetch-and-update-data-from-database-in-php/
---

# How to Fetch and Update Data From Database in PHP

## 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 DATABASE my\_db;CREATE TABLE \`customers\` (\`custId\` INT(11) NOT NULL AUTO\_INCREMENT,\`fname\` VARCHAR(255) NULL DEFAULT NULL,\`lname\` VARCHAR(255) NULL DEFAULT NULL,\`email\` VARCHAR(255) NULL DEFAULT NULL,\`created\` DATETIME NULL DEFAULT NULL,PRIMARY KEY (\`custId\`))COLLATE='latin1\_swedish\_ci'ENGINE=InnoDBAUTO\_INCREMENT=1;INSERT INTO \`customers\` (\`custId\`, \`fname\`, \`lname\`, \`email\`, \`created\`) VALUES (NULL, 'Tiago', 'Sam', 'dornelas@studio8k.com', NULL), (NULL, 'Anil', 'Kumar', 'ca.anil.kumar@gmail.com', 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:\<?php $hName='localhost'; // host name $uName='root'; // database user name $password=''; // database password $dbName = "my\_db"; // database name $dbCon = mysqli\_connect($hName,$uName,$password,"$dbName"); if(!$dbCon){ 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:\<?phpinclude 'mydbCon.php';$sql = "SELECT \* FROM customers WHERE custId='" . $\_GET\["custId"] . "'"; // Fetch data from the table customers using id$result=mysqli\_query($dbCon,$sql);$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">\<head> \<meta charset="UTF-8"> \<title>How to Fetch and Update Data from Database in PHP\</title> \<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">\</head>\<body>\<div class="container mt-2"> \<div class="page-header"> \<h2>Fetch and Update Data From Database in PHP Using Form\</h2> \</div> \<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> \<div class="form-group"> \<label for="exampleInputEmail1">Last Name\</label> \<input type="text" name="lname" class="form-control" value="\<?php echo $customer\['lname']; ?>"> \</div> \<div class="form-group"> \<label for="exampleInputEmail1">Email address\</label> \<input type="email" name="email" class="form-control" value="\<?php echo $customer\['email']; ?>"> \</div> \<button type="submit" class="btn btn-primary" value="submit">Submit\</button> \</form> \</div> \</div>\</div>\</body>\</html>

#### 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:\<?phpif(count($\_POST)>0){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$result=mysqli\_query($dbCon,$sql);echo "Form Data has been updated Successfully in PHP";}else{ 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:![](https://laratutorials.com/wp-content/uploads/2020/09/how-to-fetch-display-and-update-data-from-database-in-php-1024x358.png)

Thanks for reading this post.
