Tech

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

 

Please Subcribe and Join to get our Latest Tech and Ent. News

Buy WASSCE, BECE & Placement Checkers

Get your result checkers instantly from Jest Admissions 📝

Delivered via SMS & WhatsApp Bot within 2 seconds!

Order Checkers Here

Find Your University Fit

Use UniCutOffs to find universities and courses based on your WASSCE grades. 🎓

Try UniCutOffs Now

Streamline School Admissions

Register your school with Jest Admissions for a transparent, paperless process (Education). 📚

Register Your School

Buy Cheap Data Bundles

Stay Connected with Jest Plugs 🚀

Get affordable internet bundles from JESTDATAPLUG (MTN, AirtelTigo, & Vodafone).

Click here to buy now!

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.

KINDLY WATCH THIS VIDEO
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

<?php $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); } ?>
KINDLY WATCH THIS VIDEO
 

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

  1. Create a Database and Table: This sets up the necessary database and table to store the user information.
  2. Database Connection: This code connects to your MySQL database using the mysqli object.
  3. 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.

 

Source: GossipMotion.com

 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."

Jest

Digital Marketer || Youtuber || Software Developer || Website Developer

Related Articles

Leave a Reply

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

Back to top button