🌐
FormGet
formget.com › home › php › insert data in database using php
Insert Data in Database Using PHP | FormGet
June 6, 2014 - Can you pls tell me how to insert data into two tables from one form? For example, user registration form’s email and password should store in login table while other details are stored in user table. ... i have problem w/ my add_process form at always say that i successfully added some data to my data base .. when i check my database table the data that i added was not found what maybe the wrong in my code … · <?php if(isset($_POST['submit'])){ $first=trim($_POST['fname']); $last=trim($_POST['lname']); $middle=trim($_POST['mname']); $address=trim($_POST['address']); $sex=trim($_POST['sex']); $bday=trim($_POST['bday']); $age=trim($_POST['age']); if($first !=''||$last !=''){
🌐
IONOS
ionos.com › digital guide › websites › web development › use php to insert information into a mysql/mariadb database from an html form
Use PHP to Insert Information Into a MySQL/MariaDB Database From a HTML Form - IONOS
June 14, 2021 - This tutorial will cover how to create a basic PHP script for inserting data, and an HTML form to take user input from a webpage and pass it to the PHP script.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-insert-form-data-into-database-using-php
How to Insert Form Data into Database using PHP ? - GeeksforGeeks
July 23, 2025 - Here, we will see the complete process of inserting form data into a MySQL database using PHP, from setting up the database to writing secure and efficient code.
🌐
Ksu
people.cs.ksu.edu › ~hankley › d764 › tut06 › GopisettyPHP.html
An example to insert some data in to the MySQL database using PHP
After making a connection, a SQL query is being written to enter this data in to the MySQL database being created (“nametable”) To tell the user that the data is being entered we set the echo to "1 record added" INSERT.PHP
🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-mysql-insert-query.php
How to Insert Data Into MySQL Database Table Using PHP - Tutorial Republic
When a user clicks the submit button of the add record HTML form, in the example above, the form data is sent to 'insert.php' file. The 'insert.php' file connects to the MySQL database server, retrieves forms fields using the PHP $_REQUEST variables and finally execute the insert query to add the records. Here is the complete code of our 'insert.php' file:
🌐
DEV Community
dev.to › anthonys1760 › how-to-insert-form-data-into-a-database-using-html-php-2e8
How to Insert Form Data into a Database Using HTML & PHP - DEV Community
August 22, 2022 - Navigate to the htdocs folder and copy your index.php and insert.php files directly into this directory. This will overwrite the existing file. Now we can head over to localhost/index.php to fill out the form submit it to our database. Fill out the form and then click submit. Great! it looks like our data has been successfully stored in our DB.
🌐
W3Schools
w3schools.com › php › php_mysql_insert.asp
PHP MySQL Insert Data
PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete
Top answer
1 of 2
50

The following code just declares a string variable that contains a MySQL query:

$sql = "INSERT INTO users (username, password, email)
    VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";

It does not execute the query. In order to do that you need to use some functions but let me explain something else first.

NEVER TRUST USER INPUT: You should never append user input (such as form input from $_GET or $_POST) directly to your query. Someone can carefully manipulate the input in such a way so that it can cause great damage to your database. That's called SQL Injection. You can read more about it here

To protect your script from such an attack you must use Prepared Statements. More on prepared statements here

Include prepared statements to your code like this:

$sql = "INSERT INTO users (username, password, email)
    VALUES (?,?,?)";

Notice how the ? are used as placeholders for the values. Next you should prepare the statement using mysqli_prepare:

$stmt = $mysqli->prepare($sql);

Then start binding the input variables to the prepared statement:

$stmt->bind_param("sss", $_POST['username'], $_POST['email'], $_POST['password']);

And finally execute the prepared statements. (This is where the actual insertion takes place)

$stmt->execute();

NOTE Although not part of the question, I strongly advice you to never store passwords in clear text. Instead you should use password_hash to store a hash of the password

2 of 2
6

There are two problems in your code.

  1. No action found in form.
  2. You have not executed the query mysqli_query()

dbConfig.php

<?php

$conn=mysqli_connect("localhost","root","password","testDB");

if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}

?>

index.php

 include('dbConfig.php');

<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="style.css">

<title>test</title>


</head>
<body>

 <?php

  if(isset($_POST['save']))
{
    $sql = "INSERT INTO users (username, password, email)
    VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";

    $result = mysqli_query($conn,$sql);
}

?>

<form action="index.php" method="post"> 
<label id="first"> First name:</label><br/>
<input type="text" name="username"><br/>

<label id="first">Password</label><br/>
<input type="password" name="password"><br/>

<label id="first">Email</label><br/>
<input type="text" name="email"><br/>

<button type="submit" name="save">save</button>

</form>

</body>
</html>
Find elsewhere
🌐
Medium
medium.com › @kingsleyatanang › inserting-data-into-a-mysql-database-table-from-form-b2330b3297b7
Inserting Data into a MySQL Database Table From Form | by Kingsley Atanang | Medium
February 20, 2018 - When a user clicks the submit button of the add record HTML form, in the example above, the form data is sent to ‘insert.php’ file. The ‘insert.php’ file connects to the MySQL database server, retrieves forms fields using the PHP $_POST variables and finally execute the insert query to add the records. Here is the complete code of our 'insert.php' file:
🌐
Eli the Computer Guy
elithecomputerguy.com › 2019 › 12 › mysql-insert-records-with-html-form-and-php
MySQL – INSERT Records with HTML Form and PHP – Eli the Computer Guy
<?php $name = $_POST['name']; $age = $_POST['age']; $gender = $_POST['gender']; $servername = "localhost"; $username = "bob"; $password = "123456"; $db = "classDB"; $conn = new mysqli($servername, $username, $password, $db); if ($conn->connect_error){ die("Connection failed: ". $conn->connect_error); } $sql = "insert into students(name,age,gender) values('$name','$age','$gender')"; if ($conn->query($sql) === TRUE) { echo "ADDED: ".$name.", ".$age.", ".$gender; } else { echo "Error: ".$sql."<br>".$conn->error; } $conn->close(); ?> ... PHP is great for being able to easily create web apps that work. The problem is that it also easy to create code with major vulnerabilities that is a mess to read and try to […]
🌐
Tutorialspoint
tutorialspoint.com › php › mysql_insert_php.htm
Insert Data into MySQL Database
<html> <head> <title>Add New Record in MySQL Database</title> </head> <body> <?php if(isset($_POST['add'])) { $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if(! get_magic_quotes_gpc() ) { $emp_name = addslashes ($_POST['emp_name']); $emp_address = addslashes ($_POST['emp_address']); }else { $emp_name = $_POST['emp_name']; $emp_address = $_POST['emp_address']; } $emp_salary = $_POST['emp_salary']; $sql = "INSERT INTO employee ". "(emp_name,emp_address, emp_sal
🌐
Scaler
scaler.com › home › topics › php mysql insert query
PHP MySQL Insert Query (with Examples) - Scaler Topics
April 12, 2024 - Run the above code in your editor for a better and clear explanation. Handle the Form Submission in PHP: Create a PHP file (e.g., insert_data.php) that will receive the form data and insert it into the database.
🌐
Jotform
jotform.com › user guides › advanced features › how to send submissions to your mysql database using php
How to Send Submissions to Your MySQL Database Using PHP
February 4, 2025 - Open the PHP file in your text editor. Search for Database Config in the code and replace the values with your database information. Next, search for Data to Save and add the POST data to save in your database.
🌐
YouTube
youtube.com › cs engineering gyan
How to Insert Form Data into Database using PHP | MySQL | Xampp in Hindi - YouTube
How to Store Form Data in Database using PHP -2021 Create An HTML Form And Insert Data Into The Database using XamppStart XAMPP Server.1. Open localhost/PHPM...
Published   September 1, 2021
Views   95K
🌐
Hostinger
hostinger.com › home › tutorials › how to use php to insert data into mysql database
How to Use PHP to Insert Into MySQL Database: 2 Easy Methods
May 13, 2024 - The following line looks like this: $sql = "INSERT INTO Students (name, lastName, email) VALUES ('Tom', 'Jackson', 'tom@jackson.tld')"; The INSERT INTO is a statement that adds data to the specified MySQL database table.
🌐
Medium
medium.com › @biswajitpanda973 › how-to-insert-data-into-mysql-database-using-core-php-for-beginners-7e414986f4c5
How to insert data into mysql database using core php for beginners | by biswajit panda | Medium
February 6, 2024 - In the provided example, action="process_registration.php" indicates that when the user submits the form, the data will be sent to the process_registration.php script for further processing. we will see this later , lets create the database first
🌐
YouTube
youtube.com › watch
How to Insert Form Data into Database using PHP - YouTube
In this video, we will store data in a database submitted through HTML form. Requirements:XAMPP Server (Web Server)HTMLPHPMySQL
Published   February 21, 2023
🌐
Student Tutorial
studentstutorial.com › php › php-mysql-data-insert.php
Insert Data Into MySQL Using PHP
<!DOCTYPE html> <html> <body> <form method="post" action="process.php"> First name:<br> <input type="text" name="first_name"> <br> Last name:<br> <input type="text" name="last_name"> <br> City name:<br> <input type="text" name="city_name"> <br> Email Id:<br> <input type="email" name="email"> <br><br> <input type="submit" name="save" value="submit"> </form> </body> </html> <?php include_once 'database.php'; if(isset($_POST['save'])) { $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $city_name = $_POST['city_name']; $email = $_POST['email']; $sql = "INSERT INTO employee (first_name,last_name,city_name,email) VALUES ('$first_name','$last_name','$city_name','$email')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully !"; } else { echo "Error: " .
🌐
Tuts Make
www-tutsmake-com.translate.goog › home › php code insert/store data into mysql database from form
PHP Code Insert/Store Data Into MySQL Database From Form - Tuts Make
July 20, 2023 - In this step, you need to create one file name insert.php and update the below code into your file. The below code inserts your form data into the MySQL database table using a PHP script.