(Source Code) How to Store HTML Form Data to MySQL Database

To save user input from this form into your database, follow these steps:
1. Create a Database and Table
First, create a database and a table to store the user information. You can do this using phpMyAdmin or via a MySQL command line.
CREATE DATABASE university_db;
USE university_db;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
index_no VARCHAR(13) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
contact_number VARCHAR(10),
gender VARCHAR(6),
age INT,
email VARCHAR(100)
);
2. Connect to the Database
Create a PHP script to connect to the database.
php
3. Save the Form Data (ALL IN ONE)
Modify your existing PHP form to include the database connection and save the form data when submitted.
php
<!DOCTYPE html>
<html>
<head>
<title>University of Education Winneba, Advanced Website Design Course</title>
</head>
<body>
<?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// Database connection
$servername = “localhost”;
$username = “root”;
$password = “”; // your database password
$dbname = “university_db”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
// Prepare and bind
$stmt = $conn->prepare(“INSERT INTO students (index_no, first_name, last_name, department, contact_number, gender, age, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?)”);
$stmt->bind_param(“ssssssis”, $index_no, $first_name, $last_name, $department, $contact_number, $gender, $age, $email);
// Set parameters and execute
$index_no = $_POST[‘Index_No’];
$first_name = $_POST[‘fName’];
$last_name = $_POST[‘lName’];
$department = $_POST[‘department’];
$contact_number = $_POST[‘contact’];
$gender = $_POST[‘gender’];
$age = $_POST[‘age’];
$email = $_POST[’email’];
if ($stmt->execute()) {
echo “New record created successfully”;
} else {
echo “Error: ” . $stmt->error;
}
$stmt->close();
$conn->close();
}
?>
<center>
<h1>Welcome, Create an Account to Begin</h1>
<legend style=”background” align=”center”>DEMOGRAPHIC INFORMATION</legend>
</center>
<center>
<fieldset style=”width: 600px”>
<table border=”1″ cellpadding=”5″ cellspacing=”1″ width=”700px”>
<form action=”” method=”post” style=”width: 40%”>
<fieldset align=”center”>
<table align=”center”>
<tr>
<td>INDEX NO.</td>
<td><input type=”text” maxlength=”13″ placeholder=”501111″ title=”this is your student identity number” name=”Index_No”></td>
</tr>
<tr>
<td>FIRST NAME</td>
<td><input type=”text” name=”fName” placeholder=”Kofi” required></td>
</tr>
<tr>
<td>LAST NAME</td>
<td><input type=”text” name=”lName” title=”please write your Surname” placeholder=”Mansa” required></td>
</tr>
<tr>
<td>DEPARTMENT</td>
<td>
<select name=”department”>
<option value=”ict”>ICT DEPARTMENT</option>
<option value=”maths”>MATHEMATICS DEPARTMENT</option>
<option value=”english”>ENGLISH DEPARTMENT</option>
<option value=”science”>SCIENCE DEPARTMENT</option>
<option value=”economics”>HOME ECONOMICS DEPARTMENT</option>
<option value=”business”>BUSINESS DEPARTMENT</option>
</select>
</td>
</tr>
<tr>
<td>CONTACT NUMBER</td>
<td><input type=”number” maxlength=”10″ minlength=”10″ placeholder=”0243218853″ title=”What is phone number” name=”contact”></td>
</tr>
<tr>
<td>GENDER</td>
<td>
<input type=”radio” name=”gender” value=”Male” checked>Male<br>
<input type=”radio” name=”gender” value=”Female”>Female
</td>
</tr>
<tr>
<td>AGE</td>
<td><input type=”text” name=”age”></td>
</tr>
<tr>
<td>E-MAIL</td>
<td><input type=”email” placeholder=”htechie_menson@uew.edu.gh” title=”Provide your E-mail address” name=”email”></td>
</tr>
</table>
<center>
<button type=”submit” name=”submit”>SUBMIT</button>
</center>
</fieldset>
</form>
</table>
</fieldset>
</center>
</body>
</html>
Explanation
- Create a Database and Table: This sets up the necessary database and table to store the user information.
- Database Connection: This code connects to your MySQL database using the
mysqli
object. - Form Submission Handling: When the form is submitted, the script processes the input data, prepares an SQL
INSERT
statement, binds the parameters, and executes the statement to insert the data into the database.
Thank you for reading this article on GossipMotion.com, a premier news publishing website from Ghana. We appreciate your support and encourage you to share this article with your friends and family. Your engagement helps us continue to bring you the latest news and stories. Thank you!
"DISCLAIMER: Gossipmotion will respond to any and all take-down requests that comply with the requirements of the (DMCA). If you believe that a file that we uploaded to Gossipmotion infringes on your copyright then please contact jhuxtelloitech@gmail.com to submit a request."