Insert Data From a Form Into a Database

Now we will create an HTML form that can be used to add new records to the “Persons” table.

Here is the HTML form:


<form action=”insert.php” method=”post”>
Firstname: <input type=”text” name=”firstname”>
Lastname: <input type=”text” name=”lastname”>
Age: <input type=”text” name=”age”>
<input type=”submit”>


When a user clicks the submit button in the HTML form, in the example above, the form data is sent to “insert.php”.

The “insert.php” file connects to a database, and retrieves the values from the form with the PHP $_POST variables.

Then, the mysqli_query() function executes the INSERT INTO statement, and a new record will be added to the “Persons” table.

Here is the “insert.php” page:

// Check connection
if (mysqli_connect_errno())
echo “Failed to connect to MySQL: ” . mysqli_connect_error();

$sql=”INSERT INTO Persons (FirstName, LastName, Age)

if (!mysqli_query($con,$sql))
die(‘Error: ‘ . mysqli_error($con));
echo “1 record added”;




2 thoughts on “Insert Data From a Form Into a Database

  • October 18, 2013 at 6:42 am

    $result = mysqli_query($con,”SELECT `note` FROM `glogin_users` WHERE email = ‘$email'”);

  • October 26, 2013 at 11:05 am

    I am really loved this blog. Its an informative subject matter. It aid me quite a lot to remedy some problems. Its opportunity are so wonderful and functioning style so speedy

Leave a Reply