Swati Lathia

Learning ways

Database Connectivity

We have already learnt form element in our HTML tutorial. Since we understand that HTML only shows you how your form look like, it can not store/save your data for future reference. We can not edit or delete that data without storing anywhere. We all know that PHP enhances our website with its better connectivity to database. Here, we are using PHP with MySQL tool (phpmyadmin). For this, you just need web server (here we are using WAMP server). So let’s start with simple example of student detail.

We have created our database in phpmyadmin with database name “db_student“. Table inside this database is “tbl_stud_detail“. All the fields are : stud_id integer(5) Primary Key Auto Increment, stud_name varchar(50), stud_city varchar(50), stud_age integer (3)

After creating the table, now we are start creating HTML form from which we can insert data to tbl_stud_detail.

The following file is “stud_insert.html

<form method=post action="stud_insert.php">
	Enter Student Name :<input type=text name=t1><br>
	Enter Student City :<input type=text name=t2><br>
	Enter Student Age :<input type=number name=t3><br>
	<input type=submit name=s1><br>
</form>

Now create “stud_insert.php” file to connect it with database

<?php
//Fetch all the values from HTML form elements 
$s_nm=$_POST['t1'];
$s_city=$_POST['t2'];
$s_age=$_POST['t3'];

//Connect with server & phpmyadmin database 
$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"db_student");

//Insert query to insert record into table
$insert_qry="INSERT INTO `tbl_stud_detail` 
	(`stud_name`,`stud_city`,`stud_age`) 
	VALUES 
	('$s_nm','$s_city','$s_age')";

//Execute/Send the query to phpmyadmin
mysqli_query($conn,$insert_qry);


/*After sending insert query this page redirects to stud_data.php where a tabular format of data is displayed */
header("location:stud_data.php");

Now create “stud_data.php“. This page shows you the inserted records in a tabular format.

<?php

//Connection & database selection
$conn=mysqli_connect("localhost","root","");
$db=mysqli_select_db($conn,"db_student");

//Select required columns from table
$select="select * from `tbl_stud_detail`";
$result=mysqli_query($conn,$select);

echo "<table border=2>
	<tr>
	<th>Student Id</th>
	<th>Student Name</th>
	<th>City</th>
	<th>Age</th>
	</tr>";

//This while loop executes till the record is in the resultset (result returned from SELECT query)
while($r=mysqli_fetch_row($result))
{
        echo "<tr>
		<td>".$r[0]."</td>
		<td>".$r[1]."</td>
		<td>".$r[2]."</td>
		<td>".$r[3]."</td>
	</tr>";
}

echo "</table>";
echo "<br><a href='stud_insert.html'>Add another record</a>";
?>

This is how you can insert any data from HTML form to your phpmyadmin database table & fetch them in your webpage as per your requirement in a tabular form.

Database connectivity Videos

Creation of Database and table in MySQL (phpmyadmin)

How to fetch the record from Database table to Your webpage/browser

How to delete record from Database table

How to update record(s) in database table

Select query Example (fetching records from table)

Scroll to top