Hello friends, in this PHP MySQL Insert into Database tutorial we will use jQuery AJAX method to insert values in our MySQL database without refreshing our web page. So lets begin.
PHP MySQL with AJAX Tutorial
- You can read this tutorial or you can watch this playlist for complete detailed explanation.
What you need before starting with PHP MySQL Insert Tutorial?
- WAMP / XAMPP server (I will be using wamp)
- Notepad++ (notepad is also ok)
- Little knowledge of PHP, SQL and JavaScript
Creating Database
- First we will create a database
- Go to phpmyadmin (localhost/phpmyadmin)
- Create a database
- Create a table inside the database named users

I have created a table name users. In users I have three columns (id, username and password) as you can see above. Id is primary key with an auto increment. So we will insert only username and password with our html form.
Creating HTML Form to Insert Values in Database
- Now we will create our html form.
- You can use the below html code to create your form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html> <html> <head> <title>PHP MySQL Insert Tutorial</title> <script src='https://code.jquery.com/jquery-2.1.3.min.js'></script> </head> <body> <form action='insert.php' method='post' id='myform' > <p> <input type='text' name='username' placeholder='user name' id='username' /> </p> <p> <input type='text' name='password' placeholder='password' id='password' /> </p> <button id='insert'>Insert</button> <p id='result'></p> </form> </body> </html> |
- As you can see we have one form with action = insert.php i.e. when we click on the button we will be redirected to insert.php.
- The method is post and we have also given an id to our form which is myform.
- The form has two inputs, one for username and other for password with name and id attribute.
- We will use the id attribute to access the values from jQuery. So we have also added the jQuery file inside the head tag.
- And at last we have a p element with an id result. This is to show the success message after successful insertion.
- The above form will generate following output.

Creating PHP Script to Insert Values in Database
- Create a file named insert.php
- First we will connect to our database.
- For connecting we will define some constants.
1 2 3 4 5 6 7 | <?php define('HOST','localhost'); define('USERNAME', 'root'); define('PASSWORD',''); define('DB','test'); |
- Now we will connect to our database using the constants we defined.
1 2 3 | $con = mysqli_connect(HOST,USERNAME,PASSWORD,DB); |
- Get the values which are coming from our form using post method.
1 2 3 4 | $username = $_POST['name']; $pass = $_POST['pass']; |
- Finally we will insert these values into our database
1 2 3 4 5 6 7 8 | $sql = "insert into users (name, password) values ('$username','$pass')"; if(mysqli_query($con, $sql)){ echo 'success'; } ?> |
- Your final php script for php mysql insert project is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php define('HOST','localhost'); define('USERNAME', 'root'); define('PASSWORD',''); define('DB','mydatabase'); $con = mysqli_connect(HOST,USERNAME,PASSWORD,DB); $username = $_POST['name']; $pass = $_POST['pass']; $sql = "insert into users (username, password) values ('$username','$pass')"; if(mysqli_query($con, $sql)){ echo 'success'; } ?> |
- Now try running your project.
- It is working fine but we are getting redirected to insert.php. Our task is to insert without refreshing our redirecting the page. For this we will use jQuery AJAX method.
Using jQuery AJAX Methods to insert values in database without refreshing the page
- Create a new file name insert.js
- Add your insert.js to your html file below the form
1 2 3 | <script src='insert.js'></script> |
- First we will stop our form from getting redirected to insert.php
1 2 3 4 5 | $('#myform').submit(function(){ return false; }); |
- Try clicking insert button now, nothing will happen. 😛
- Now we will execute our php script in background using jQuery post method.
1 2 3 4 5 6 7 8 9 | $.post( 'script to execute', 'values to send', function(result){ //get the output of the script } ); |
- We will use the above post method inside the click function of our button
- So our final insert.js will be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $('#myform').submit(function(){ return false; }); $('#insert').click(function(){ $.post( $('#myform').attr('action'), $('#myform :input').serializeArray(), function(result){ $('#result').html(result); } ); }); |
- Try running your project now. It is working fine the values are getting inserted in database without refreshing our page.
- You can also download the project source from the link given below. And if you are having any confusion or doubts following the steps feel free to comment. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
Login & Register form
Your way inserting data into database is not working when the file of insert.php saved in deepest folder. so “return false” failed!
<form action='folder1/folder2/folder3/folder4/insert.php' ……..
Could you give me the solutions..?
Thx, Rgds
Haryo
Ups Sorry, I made a lil’ mistake on saving insert.js
🙂 Good Job! it is working fine…
Thanks, I will use this way.
Rgds,
Haryo
Well done. This is what I wanted all along.
Just came from VB Programming to the advanced Web bassed application.
Thank you. You have made my day./ career.
Thank you keep visiting 🙂
Thank you for the great job. I am able to get the database updated and success message. But it doesn’t empty the form. so same data can be submitted over and over. Ho can I fix that?
use the val function to empty the form $(‘#inputfield’).val(”);
Hi, nice work!
Where should I add this code $(‘#inputfield’).val(”); to clear the form?
Thank you
I think you use this code:-
$(‘#myform’).submit(function(){
return false;
});
$(‘#insert’).click(function(){
$.post(
$(‘#myform’).attr(‘action’),
$(‘#myform :input’).serializeArray(),
$(‘#inputfield’).val(“”),
function(result){
$(‘#result’).html(result);
}
);
});
this is owesome.
help me with a tutorial to insert data from a listview to mysql database
You can do it with a little changes..
code are awsom plus helpful. But I have a problem, still am reaching to insert.php.
What do you mean??
I mean to say ..
ham chate hai ki insert.php meh na jaye when we click on insert button .
index.html meh hi sara process hojaye.
that is what we are doing here.. Everything is happening on index.html..
I did what all you have done here. Starting from creating index.php then insert.php and insert.js.
I added insert.js below the form inside the index.php and written action = “insert.php”.But still when I click on insert button, it shows success message in insert.php page which I don’t want. If you have a tutorial video in youtube then please let me know.
Thank You.
connect with me on facebook.com/Probelalkhan and i will try to help u
I am getting the same problem
okh bro
You r the best!!!!!!!
Hey I got this nearly working. You see I am using more than one form on a page… an add to cart button on serveral products.
I have changed the form id to form id from myform to myform12 or what ever the prod code is and have also changed the insert id accordingly as well. Then in the js code I have changed all of the ids to match the ones in my form.
The problem i get is that my input values are not passing to the insert script. I get an Notice: Undefined index on the variables in the insert script 🙁
Any ideas?
how to upload multiple images in database and image folder without page refresh?
hi
i have one template in which i can add rows dynamically through java script.
now i wanted to add each row as a one record in my database (php mysql).
i m beginner to php so cant handle it… can u pls help
Thank you nice example
Hello ,
I have 1 page and four form tag which are step by step form i want to insert one by one page using ajax i serialize the form by its id but i get output with original text but with that original text there are add “+++++” sign into output,so i can not get proper output how can i fix the problem?
nice code
thanks its working
Thank you it’s working ^^
My best wishes.
Your tutorials are helpful for my life
Thanks
use append function in javascript
so you can add dynamically row add
thank you so much.
this is exactly what i have been looking for.
but what about if i have two form on one page. how do i handle that.