Skip to main content

Database Connectivity in PHP


  • Introduction
  • Create new Database and Table
  • create a feedback.html file to take values from Users
  • Create feedback.php file to store theese values into database 




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 .




Creating New Database and Table



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

* Query Syntex to create "feed" Table :- 
  CREATE TABLE feed
  (
  name varchar(30),
  message varchar(500),
  );



feedback.html File



file name - feedback.html 

Save it .

<html>
<head>
<title>
Feedback Form 
</title>
</head>
<body>
<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> 
</table>
</form>
</body>
</html>

 NOTE  :- 

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





feedback.php file



<?php


  $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;


  }


  $conn->close();


  ?>
 



Popular posts from this blog

How can we connect a Printer to Computer

We can connect any printer to a computer by following just below written simple steps -

1.   Plug in the printer in power socket.
2.   Turn on the printer.
3.   Turn on the Computer .
4.   Connect the printer to the computer / laptop using USB cable.
5.    Printer drivers installation process starts on computer or install by the CD came with printer.
6.    Go to control panel --Devices and printer
                                                     |
                                            Click on " Add the device / printer "
                                                     |
                                             Select Printer
                                                     |
                                              Click on Add This
7 .       Done ! ! !



How to prevent computer or laptop from overheating

Overheating is a big problem for  computer or laptop but it can be solved easily .Some steps to prevent by overheating - 1. Keep the computer clean and remove the dust not only outside but also inside from fan , motherboard or entire cpu .2. Keep the computer surrounding cool . Low temperature of room also prevent computer by overheating .3. Place the computer at open place .
4. Do not overclock computer .

Let's start a blog

Okay let's start to create a blog . First thing is the right platform to start a blog . We listed some of very popular blogging platforms all over the world with the features and uniqueness , they provide .

We do not say to you to choose any specific platform from the list because all are trusted and used by millions and all give the best results  .

Just give a look to our blogging platforms list and enjoy blogging with any one as you like . Do not confuse and irritate because all have their own pros and cons . 
Some platforms to start a blog  -

1. Blogger -


Blogger is the oldest blogging platform in or list . It is owned by Google . Blogger is a self hosted service means google takes the responsibility to securely host the written data . 
Blogger was started for the passionates about something that they like to do . 
Blogger is completely free to start and setup a blog with .blogspot.com ( like example.blogspot.com ) extension but it can be changed by purchasing own domain .
Goo…