how to retrieve data from database in html form using PHP

Today, We want to share with you how to retrieve data from database in html form.In this post we will show you how to fetch data from database in php and display in html table, hear for Retrieve data from database and display in php form we will give you demo and example for implement.In this post, we will learn about how to fetch data from database in php and display in textbox with an example.

Retrieve data from database and display in php form

As we already display “how to insert data in mysql using php form“. But if want to edit some details in that form how will We do that? must We quickly update form without viewing the old data? Or must We check my old data first as well as then update or change each all data? also you can check how to fetch data from database in php using mysqli?

Answer is clear that early We have to look at old data then We will update or change that. But how to get data and display in HTML form
Don’t worry in this Article We are going to show how to retrieve data from database with display in php form.

We are returning Our foregoing example of insert data. In that Article We Made a HTML form with product_name and content field only. So this time We are not going to make MySQL database as well as table because We already Made both of them in Our preceding Article.

Create database connection:

database.php

<?php 
$host = "localhost";
$user = "root";
$pass = "[email protected]";
$db = "shop_0007";
 
$conn = mysqli_connect($host,$user,$pass, $db);
if(!$conn)
{
die("Sorry, Dear Your Connection failed: " . mysqli_connect_error());
}
?>

Retrieve Data from Database:

For retrieve data from MySQL the SELECT Query statement is used. We can get data from each Table DB column or all column of a DB table.

To get selected each column data from database Table the MySQL query is

SELECT column_name,column_name FROM products;

To get all the column data from a table the MySQL query is

SELECT * FROM products;

Retrieve Data From MySQL Using PHP

<?php 
	$sqlQQ = "select * from products where id = 1";
	$getResults = mysqli_query($conn, $sqlQQ);

	$getProductList = mysqli_fetch_assoc($getResults);
?> 

In above php source code We get the database record which id is 1. using mysqli_fetch_assoc() function will return that specific database table record as an array datatype (which holds product_name and details) and We save that array in $getProductList variable.

FORM HTML:

Now We get the database record from the database Table This is a time to display each DB table column like as product_desc, name, title, price, handle, brand or many more data into its respective all DB fields.

<div class="container">
	<h3>Add Product</h3>
	<form action="" method="post">
		<input type="text" name="product_name" value="<?php echo $getProductList['product_name']?>" required>
		<textarea cols="40" placeholder="Product Description" rows="8" name="product_desc" required><?php echo $getProductList['product_desc']?></textarea>
		<button type="submit" name="submit">Submit</button>
	</form>
</div> 

Included CSS:

body{font-family:verdana;}
.container{width:500px;margin: 0 auto;}
h3{line-height:19px;font-size:19px;}
input{display:block;width:360px;height:19px;margin:11px 0;}
textarea{display:block;width:360px;margin:11px 0;}
button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff}

Complete Source Code

<?php 
$host = "localhost";
$user = "root";
$pass = "[email protected]";
$db = "shop_0007";

$conn = mysqli_connect($host,$user,$pass, $db);
if(!$conn)
{
	die("Connection failed: " . mysqli_connect_error());
}

?>

<?php 
	//Retrieve Data From MySQL Using PHP

	$query_sql = "select * from products where id = 1";
	$getResults = mysqli_query($conn, $query_sql);
	//fetch Product row
	$getProductList = mysqli_fetch_assoc($getResults);
?> 



<!DOCTYPE html>
<html>
<head>	
<title> Get or Retrieve data from database and display in php form - www.pakainfo.com</title>

<style>
	body{
		font-family:verdana;
	}
	.container{width:500px;margin: 0 auto;}
	h3{line-height:19px;font-size:19px;}
	input{display:block;width:360px;height:19px;margin:11px 0;}
	textarea{display:block;width:360px;margin:11px 0;}
	button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff}
</style>

</head>	
<body>

	<div class="dsp container">
	<h3>Edit Product</h3>
	<form action="" method="post">
	<input type="text" name="product_name" value="<?php echo $getProductList['product_name']?>" required>
	<textarea cols="40" placeholder="Product Description" rows="8" name="product_desc" required><?php echo $getProductList['product_desc']?></textarea>
	<button type="submit" name="submit">Submit</button>
	</form>
	</div> 


</body>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to retrieve data from database in html form.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   ccavenue payment gateway integration in php demo
  2.   Responsive Dropdown Navigation Bar
  3.   Laravel Eloquent "WhereNotIn" Query Example
  4.   jquery ajax call php function with parameters
  5.   Strong Random Password Generator Using Pure JavaScript
  6.   Retrieve Data From MySQL Using PHP
  7.   pagination code in php with next and previous demo
  8.   WordPress AJAX URL GET POST Call PHP Function
  9.   JavaScript Integrate CKEditor in Html Page
  10.   How to Convert date string to date PHP