How to insert html form data in database | connect html with mysql database with PHP

 















First of all let's create a form to get data from user.

 

Here is the code of HTML form with CSS, you can copy this or may use your own. it's better to use this code for the first time of practice.


<!DOCTYPE html>
<html>
<style>
input[type=text],input[type=password], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

div {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
  width: 50%;
}
</style>
<body>


<div>
  <form action="form.php" method="post">
    <label for="fname">Email</label>
    <input type="text" name="email" placeholder="Enter Email">

    <label for="lname">Username</label>
    <input type="text" name="username" placeholder="Enter Username">

    <label for="lname">Password</label>
    <input type="password" name="password" placeholder="Enter Password">

 
    <input type="submit" value="Submit">
  </form>
</div>

</body>
</html>




Now let's get this this values of form using PHP to store in database. here is the PHP code.

Form.php

<?php
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

include 'config.php';

$sql = "INSERT INTO names(email, username, password)VALUES('$email','$username','$password')";
$result = mysqli_query($conn, $sql);

if($result){
   echo"Data Saved Successfully....!";
}
?>


We have successfully get the form data using POST and insert in our database method which is a secure method.

Now our next steps is to create database. we are using phpmyadmin as a database server.

To use phpmyadmin download Xampp Server in your PC. 


Now, watch this video to create database and table in phpmyadmin.




OR

You can paste this SQL code here instead of watching video.


SQL Code for creating table under "try" database.

CREATE TABLE names (
    id int NOT NULL AUTO_INCREMENT,
    email varchar(255) NOT NULL,
    username varchar(255),
    password varchar(255),  
    PRIMARY KEY (id)
);



You have successfully create database now let's connect it with our HTML.


For connection let's create a config.php file to do configuration. here is the code for configuration.

config.php

<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "try";

$conn = mysqli_connect($host, $user, $pass, $dbname) or die();
?>





For more information you can read this documentation of PHP and MySQL.


PHP:

PHP is a server-side programming language used to create dynamic web pages and static sites. It is trendy for the beck-end due to many built-in tools and modules for developing various applications. It is open source and licensed. Website development in PHP is available for both experienced developers and beginners. PHP is a simple language. You can make your blog in just two to three weeks of learning. Plus, on a freelance, it is more in demand.

Key features of PHP:

  • open-source server-side scripting language
  • cross-platform
  • short learning curve
  • in-built support for working with MySQL

Disadvantages:

  • used primarily in web development
  • weak typing
  • awkward standard library




MySQL:

MySQL is an open-source relational database management system. Its name is a combination of "My", the name of co-founder Michael Widenius's daughter, and "SQL", the abbreviation for Structured Query Language. It is a relational database management system based on SQL – Structured Query Language. The application is used for a wide range of purposes, including data warehousing, e-commerce, and logging applications.

Post a Comment

0 Comments