PHP Create HTML form to Insert data into MySQL database

How to make an html form that stores data inside MySQL db tables in php.

Submitting text data through html form into MySQL database is very important part of PHP because when we are submit our first form data to MySQL db then there are new parts of php begins like MySQL database connectivity, getting form values using $_POST method. In this tutorial we are simply making a html input form and a submit form button. Now in php section we are setting up button click condition including dbconfig.php file. So here is the complete step by step tutorial for PHP Create HTML form to Insert data into MySQL database.

android-project-download-code-button

PHP Create HTML form to Insert data into MySQL database.

Code for insert-single-data.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Create HTML form to Insert data into MySQL database</title>
</head>

<body>

<table>
<form action="" method="post">
<tr>
<td> Name: </td><td><input type="text" name="name"></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td></tr>

</form>
</table>



<?php
if(isset($_POST["submit"]))
{
 
 //Including dbconfig file.
include 'dbconfig.php';
 
$name=$_POST["name"];

mysql_query("INSERT INTO demo_table (Name) VALUES ('$name')"); 

echo " Added Successfully ";

}

 ?>

</body>
</html>

Code for dbconfig.php file.

 <?php

//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);



?>

Screenshot:

PHP Create HTML form to Insert data into MySQL database

Click here to download PHP Create HTML form to Insert data into MySQL database project with source code files and MySQL db.

Leave a Reply

Your email address will not be published. Required fields are marked *