Storing HTML Form Input in a MySQL Database Using PHP

On this page you’ll learn how to seamlessly store data from an HTML form into a MySQL database using PHP

in Node.JS, AWS


Skip to section:





<?php $servername = "{ENDPOINT-TO-MYSQL-DATABASE}"; $username = "{USERNAME-FOR-DATABASE}"; $password = "{PASSWORD-FOR-DATABASE}"; $dbname = "{DATABASE-NAME}"; $vemail = $_POST['{ID-OF-HTML-FORM-INPUT(email)}']; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; $sql = "INSERT INTO subs (firstName, email) VALUES ('', '{$vemail}')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } header("Location: https://computersciencex.com/indexSuccess.html", true, 301); $conn->close(); ?>

Output:

Sign up for our email subscription

We'll never share your email with anyone else.

This is a very simple HTML form that gets the user to enter an email address and then it will 'POST' that data to the 'action_page.php' file so that we can use/manipulate the data that the user has provided how we want using PHP.