How to Create an HTML Form that Stores Data in a MySQL Database Using PHP

Creating an HTML form that stores data in a MySQL database using PHP is a great way to collect information from users and process it in a database. By following some simple steps, you can create a form that is easy to use and stores data securely in a MySQL database. In this article, we will go over the steps involved in creating an HTML form that stores data in a MySQL database using PHP.

Step 1: Setting up the Database

Before we can create an HTML form that stores data in a MySQL database using PHP, we need to set up the database. You can use a tool like phpMyAdmin to create a new database and a table to store the data. The table should have fields for each piece of information you want to collect from the user. For example, if you are creating a form to collect user information, you might have fields for Name, Email, and Phone Number.

Step 2: Creating the HTML Form

Once you have set up the database, you can create the HTML form. You can use any text editor to create the form. The form should have input fields for each piece of information you want to collect from the user. You should also include a submit button to allow the user to submit the form.

Step 3: Processing the Form Data with PHP

Next, you will need to create a PHP script to process the form data and store it in the database. The PHP script should connect to the database, retrieve the data from the form, and insert it into the database. Here is an example PHP script that you can use as a starting point:

<?php
$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

If ($conn->connect_error) {

Die("Connection failed: " . $conn->connect_error);

}

// Retrieve data from form

$name = $_POST['name'];

$email = $_POST['email'];

$phone = $_POST['phone'];

// Insert data into database

$sql = "INSERT INTO MyGuests (name, email, phone)

VALUES ('$name', '$email', '$phone')";

If ($conn->query($sql) === TRUE) {

Echo "New record created successfully";

} else {

Echo "Error: " . $sql . "
" . $conn->error;

}

$conn->close();

?>
This script connects to the database, retrieves the data from the form using the $_POST variable, and inserts the data into the database using an SQL query.

Step 4: Displaying the Data

You may want to display the data that has been stored in the database. You can use PHP to retrieve the data from the database and display it on a web page. Here is an example PHP script that retrieves the data from the database and displays it in a table:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die("Connection failed: " . $conn->connect_error);
}
// Retrieve data from database
$sql = "SELECT name, email, phone FROM MyGuests";
$result = $conn->query($sql);
If ($result->num_rows > 0) {
// output data of each row
Echo "
";
While($row = $result->fetch_assoc()) {
Echo "";
}
Echo "
<table>
<tbody>
<tr>
<th>Name
<th>Email
<th>Phone
</tr>
<tr>
<td>" . $row["name"]. "
<td>" . $row["email"]. "
<td>" . $row["phone"]. "
</tr>
</tbody>
</table>
";
} else {
Echo "0 results";
}
$conn->close();
?>
This script retrieves the data from the database using an SQL query and outputs it in an HTML table.

Creating an HTML form that stores data in a MySQL database using PHP is a simple process that can be accomplished in just a few steps. By following the steps outlined in this article, you can create a form that is easy to use and stores data securely in a database. With a little bit of PHP programming, you can even display the data that has been stored in the database on a web page.

–>