Connect HTML form with a Database

Create an HTML Form to Stores Data in a MySQL Database Using PHP

Connect HTML form with a Database

2019 AL Southern Province ICT Paper II-  B Question 06

  • Run XAMMP Server
  • Follow URL – http://localhost/dashboard/
  • phpmyadmin  – http://localhost/phpmyadmin/
  • Create a Database                    Eg: Sajeewa2019
  • Create a Table                           Eg: devices


Name  & Data Types

IMEI – Text

WifiMAC – Text

Model –  Text

IsImported – Boolean

TelePhoneNo – Text

  • Create php file   –  Eg : sajeephp


            $host = “localhost”;

            $dbusername = “root”;

            $dbpassword = “”;

            $dbname = “sajeewa2019”;

// Create connection

$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);

// Check connection

if ($conn->connect_error) {

    die(“Connection failed: ” . $conn->connect_error);}



            $sql = “INSERT INTO devices(IMEI,WifiMAC,Model,IsImported,TelePhoneNo)

            values (‘”.$_POST[‘IMEI’].”‘ , ‘”.$_POST[‘WifiMAC’].”‘,'”.$_POST[‘Model’].”‘,



if ($conn->query($sql)==TRUE)


                                    echo “New Record is Inserted”;


                        else      {

                                    echo “error”.$sql.”<br>”.$conn->error;




  • Create html file      –   Eg : sajee.html



                                <title>form site</title>



<form method=”POST” action=”sajee.php”>

<label>IMEI</label><input type=’text’ name=’IMEI’/>

<label>WifiMAC</label><input type=’text’ name=’WifiMAC’/>

<label>Model</label><input type=’text’ name=’Model’/>

<label>IsImported</label><select name=”IsImported” ><option value=”Y”>Yes</option>

<option value=”N”>No</option></select>

<label>TelePhoneNo</label><input type=’text’ name=’TelePhoneNo’/>

<input type=’submit’ value=’submit’><br>




  • Go to xammp location à htdocs           Eg : D:\xampp\htdocs
  • Create a folder and move .php & .html files
  • Follow URL –



  • Fill & Click Submit
  • If all connection correct,
  • Open devices table, See Data stored

Try this to practice SQL & PHP connection With HTML

Click HERE to Download Pdf…

Download Powerpoint presentation : Click Here

Download Powerpoint as a PDF : Click Here

Please follow and like us:

Related posts

Leave a Comment