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 🙂
[sociallocker id=”1372″] [download id=”1400″]Â [/sociallocker]