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

Eg:

Name  & Data Types

IMEI – Text

WifiMAC – Text

Model –  Text

IsImported – Boolean

TelePhoneNo – Text

  • Create php file   –  Eg : sajeephp

<?php

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

else

{

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

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

‘”.$_POST[‘IsImported’].”‘,'”.$_POST[‘TelePhoneNo’].”‘);”;

}

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

                        {

                                    echo “New Record is Inserted”;

                        }

                        else      {

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

                                    }

                        $conn->close();          

?>

  • Create html file      –   Eg : sajee.html

<html>

                <head>

                                <title>form site</title>

                </head>

<body>

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

</form>

</body>

</html>

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

http://localhost/s******walisundara/

http://localhost/s*****walisundara/sajee.html

  • 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:
error

Related posts

Leave a Comment