3 Steps To Save HTML Form Into Database In PHP MySQL

Welcome to a beginner’s tutorial on how to save an HTML form into the database with PHP and MySQL. So you have finally come to fight the boss stage of full-stack programming – Combining HTML, PHP, and MySQL to save a submitted form into the database.

Saving an HTML form into the database is a simple 3-step process.

  1. Create a database table with the fields you want to capture.
  2. Create the corresponding HTML form, that submits to a PHP script.
  3. In the PHP script, open a database connection and insert the submitted fields.

Yep, it’s really that simple but read on for an actual example!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

SAVE HTML FORM INTO DATABASE

All right, let us now get started on an example of saving an HTML form into the database.

 

TUTORIAL VIDEO

 

STEP 1) CREATE THE DATABASE TABLE

1a-dummy.sql
CREATE TABLE `survey` (
  `email` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `color` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
ALTER TABLE `survey`
  ADD PRIMARY KEY (`email`);

For this example, we will be doing a simple “survey” on the favorite color that the users like; We will be capturing 3 fields – The email, name, and the color itself.

 

 

STEP 2) CREATE THE HTML FORM

2-form.html
<?php
// (A) SAVE SURVEY FORM ON SUBMIT
if (isset($_POST["email"])) { require "3-save.php"; }
?>
 
<!-- (B) SURVEY FORM -->
<form method="post">
  <label>Email</label>
  <input type="email" name="email" required>
  <label>Name</label>
  <input type="text" name="name" required>
  <label>Favorite Color</label>
  <input type="text" name="color" required>
  <input type="submit" value="Go!">
</form>

Shouldn’t have any trouble with this one too. This is an HTML form with exactly what we want to capture – The email, name, and favorite color. Take note of that if (isset($_POST["email"])) though, we will only include the “save to database” PHP script only when the form is submitted.

 

 

STEP 3) PHP SAVE FORM INTO DATABASE

3-save.php
<?php
// (A) DATABASE CREDENTIALS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
 
// (B) CONNECT TO DATABASE
$pdo = new PDO(
  "mysql:host=".DB_HOST.";charset=".DB_CHARSET.";dbname=".DB_NAME,
  DB_USER, DB_PASSWORD, [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
 
// (C) INSERT
$stmt = $pdo->prepare("INSERT INTO `survey` (`email`, `name`, `color`) VALUES (?, ?, ?)");
$stmt->execute([$_POST["email"], $_POST["name"], $_POST["color"]]);
 
// (D) RESULTS
echo "OK";

I don’t think this one needs a lot of explanation either – Connect to the database and do an SQL INSERT. That’s all, really.

 

 

EXTRA) SEND EMAIL NOTIFICATION

3-save.php
/* (X) SEND EMAIL
$message = "";
foreach ($_POST as $k=>$v) { $message .= "$k : $v\r\n"; }
mail("admin@site.com", "Survey Received", $message);
*/

Need to send out an email? Just add a few more lines after the database insert is done.

 

 

EXTRAS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “3 Steps To Save HTML Form Into Database In PHP MySQL”

  1. Great article, I am a beginner – What changes would need to be made for using PostgreSQL instead of MySQL? Thanks!!

Leave a Comment

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