Skip to main content

Database Connectivity in PHP

Content -
  1. Introduction
  2. Create new Database and Table
  3. create a feedback.html file to take values from Users
  4. Create feedback.php file to store theese values into database 

1. Introduction

When we create any form in our PHP Website and want to connect this form to store the information in server Databases , we need to know how php webpage can be connected to database and how can we store the information successfully .

As an example , To add Feedback or Message Form in PHP website -

1. Create Database and Table

2. Creating HTML form .

3. Write database Connectivity Code

Using this process you can connect all types of HTML Form to the SQL or MySQL Database by little change in Form Code and Database Connectivity Code .

2. Creating New Database and Table

Go to your SQL or MySql Databse and Run theese queries : -
* Query Syntex to create a new database named "feedback" :-

* Query Syntex to create "feed" Table :- 

name varchar(30),
message varchar(500),

3. feedback.html File

file name - feedback.html

Save it .

Feedback Form
<h2 align=center> Feedback Form </h2>
<form action="feedback.php" method=post >
<table align=center border=1 bgcolor=yellow width=25% height=50%>
<tr> <td> Name </td> <td> <input type=text name="nm" > </td> </tr>
<tr> <td> Message </td> <td> <textarea name="message" rows=4 cols=30> </textarea> </td> </tr>
<tr> <td colspan=2 align=center> <input type=submit value="Submit"> </td> </tr>


In this Form the value of Name and Message are passed to "feedback.php" page .

4. feedback.php file

$name = $_POST["nm"];
$message = $_POST["message"];
$servername = "localhost";
$username = "Write Your UserName Here";
$password = "Write Your Password Here";
$dbname = "Write Database Name ,here feedback ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$sql = "INSERT INTO feed (name,message) VALUES ('$name', '$message')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;

Now you can upload the Database connectivity script to your hosting server or can use as localhost using Xampp Server or Wamp Server .