Insert Data into Database Using PHP and MySQL
Insert data in to a database using PHP and MySQL for safekeeping. Which makes a website able to save the data users enter into the forms, such as the user‘s names, emails, and passwords.
PHP then uses an SQL INSERT command to send the data through to a MySQL database. As a consequence of this website data isn‘t lost after refreshing a page.
Why Inserting Data Is Important
Websites Everyday, gather data.
Registered users
Form submits by the user.
Users create accounts
If data is not saved, the website becomes useless. Hence, data-entry is a basic and needed skill.
Requirements Before Inserting Data
Please ensure the following steps are taken before entering data.
PHP is related to MySQL
There is a already database
We create a table
A form is a way of collecting user input.
Then you can put in data with no mistakes, when those are ready.
Example Database Table
Below is a simple users table.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
password VARCHAR(100)
);
This table stores basic details of users.
HTML Form to Collect Data
First, create a form to get input from users.
<form method="post" action="insert.php">
<label>Name</label><br>
<input type="text" name="name" required><br><br>
<label>Email</label><br>
<input type="email" name="email" required><br><br>
<label>Password</label><br>
<input type="password" name="password" required><br><br>
<input type="submit" name="submit" value="Register">
</form>
POST method does not show the data in URL
Insert Data Using PHP
Now send the form data to the database.
<?php
include "db.php";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO users (name, email, password)
VALUES ('$name', '$email', '$password')";
if (mysqli_query($conn, $query)) {
echo "Data saved successfully";
} else {
echo "Error saving data";
}
}
?>
This code runs an INSERT query and saves the data.
How the Insert Process Works
The steps are simple.
The user completes the form
Form submits data to php
PHP communicates data to MySQL
MySQL records are stored in the following table.
Consequently, the data remains stored.
Password Safety
Passwords should never be stored as plain text.
Passwords should be hashed before storing.
$password = password_hash($password, PASSWORD_DEFAULT);
This secures user accounts.
Common Beginner Errors
Most of the beginners will suffer these problems.
Wrong connection to database
Incorrect table name.
Absent form validation
Saving raw passwords
Resolve and Fix these issues for better quality code.
Best Practices
Follow these:
Validate user input
Use POST for forms
Hash passwords
Use prepared statements a bit later in a while.
Send messages that all are clear
These rules help in real projects.
Summary
Insert data in a database with PHP and MySQL to store user input on disc permanently. In this way, forms, PHP and INSERT SQL queries are used. When correctly used, it contributes to build registration forms, contact forms, login systems, etc.
Check out our resources!
- Bootstrap Templates: Explore our Bootstrap Projects section.
- Free E-Books: Download your Free E-Books here.


