php

How to Create an HTML Form That Store Data in MySQL Database

The primary purpose of creating and using HTML forms is to collect information about the users. Therefore, it is unproductive to update information collected from HTML forms in a database manually.

This quick and straightforward tutorial will discuss how to save information from an HTML form to a MySQL database using PHP.

Step 1: Set up Environment

The first step is setting up a working environment for PHP and MySQL. For this tutorial, we will use AMPPS by Softaculous. You can opt for other options, such as Xampp or Wamp.

Open your browser and navigate to the resource:

https://ampps.com/downloads/

Select the installer version for your operating system:

Once downloaded, select the installer package and follow the instructions to set up Ampps on your system.

Once Ampps is installed, run it. Start the Apache and MySQL services as shown:

Using Ampps, you should have a local web server running Apache, PHP, and MySQL. Now, we can create a database.

Minimize the Ampps window and close to stop the Apache and MySQL services.

Step 2: Create Database

The second step is creating and setting up a simple database to interact with using PHP. Open your browser and navigate the http://localhost/phpmyadmin website.

Once you login to the Phpmyadmin page, select the new option to add a new database, as shown in the image below:

In the next window, set the database name and click create. In this example, we call the database html_form:

Now that the database is initialized, we can create a database schema.

Step 3: Create Database Table

After database creation, PhpMyAdmin will redirect you to a table creation page. You can create tables and fill the information in columns and rows.

Start by creating a name for your table. In this example, we will call it simple_form. Set the column value as four since we will have three input fields in the HTML form:

Click Go to create the table and column information. Once completed, you should have sample columns with details input as shown:

Fill in the column information, such as data types and more. The following example information is provided:

Column Name Type Length/Values Index/A_I
id INT PRIMARY/True
username VARCHAR 255
email VARCHAR 255
comment TEXT 255

An example schema info is as shown:

Click Save to create the tables and columns as specified.

Step 4: Create Database Privileges

Select the privileges tab and click on add account to create a new MySQL account.

Enter the login details, such as username and password, as shown in the example image below:

Step 5: Create HTML Form

Now that we have the database configured, we need to create a HTML form to collect information.

Open the Ampps www directory and create a new directory. Give it any preferred name. In this example, we set the directory’s name as html_forms.

Create an index.html form and paste your html form code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>HTML Form</title>

<link rel="stylesheet" href="style.css">

</head>

<body>

<form method="POST" action="submit.php">

<label>Username: </label><br>

<input type="text" name="username" placeholder="my_username" /> <br>

<label>Email: </label> </br>

<input type="email" name="email" placeholder="[email protected]" /><br>

<label>Comment</label> </br>

<textarea name="comment" id="" cols="30" rows="10"></textarea>

<br>

<button> Submit </button>

</form>

</body>

</html>

Step 6: Fetch Form Info With PHP

The next step is to create a submit.php file that fetches information from the HTML form and updates it in the database, as shown in the following code:

<?php

if(isset($_POST['submit']))
      {
          $usernamename = $_POST['username'];
          $email = $_POST['email'];
          $comment = $_POST['comment'];

          $host = "localhost";
          $username = "forms";
          $password = "password";
          $db = "html_form";
          $conn = mysqli_connect($host, $username, $password, $db);
if (!$con)
          {
die("Connection failed!" . mysqli_connect_error());
          }
          $query = "INSERT INTO simple_form (id, username, email, comment VALUES $username, $email, $comment";
          $run = mysqli_query($conn, $query);
if (run)
          {
echo"Success";
          }
mysqli_close($conn);
      }


?>

We create a connection to the database and perform an SQL query using the mysql_query function in the code above.

Once you submit the form, the submit.php file should run, and the data from the form inserted into the database.

Conclusion

This short article provided a step-by-step guide on how to save information from an HTML form into a database using PHP. In addition, this article provided instructions on setting the Ampps program. We hope you found this article helpful. Check out other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list