Insert data in MySQL with HTML form through PHP

In this article I am going to explain about insert data in MySQL with help of HTML form.
  • 1964

Insert data from a form into database

By the help of html form, you can also insert value in Mysql database table. For inserting data into MySQL database in PHP through HTHL form, follow the following steps:

  • First create a PHP application

<html>

<body>

<?php

$con = mysql_connect("localhost","gupta","sharad");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("Mcn_Solution", $con);

 

$sql="INSERT INTO Emp (Fname , LastName, Id) VALUES ('$_POST[Fname]','$_POST[LastName]','$_POST[Id]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "One record added";

 

mysql_close($con);

?>

</body>

</html>

  • After it, create a html form

<html>

<body>

<form action="add.php" method="post">

Firstname: <input type="text" name="Fname" />

Lastname: <input type="text" name="LastName" />

Id: <input type="text" name="Id" />

<input type="submit" />

</form>

</body>

</html>

</html>


Output:

php inser data.jpg

You may also want to read these related articles :
here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2019 DotNetHeaven. All rights reserved.