Skip to main content

PHP - Simple Insert MySQLi Data Using AngularJS

In this tutorial we will create a Simple Insert MySQLi Data Using AngularJS. This code will submit the MySQLi POST data when the user click the submit button. The code use angular directives to insert the form data that enable to send a POST request to the MySQLi server. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And this is the link for the bootstrap that i used for the layout design

Lastly, this is the link for the AngularJS

Creating Database

Open your database web server then create a database name in it db_angular_insert, after that click Import then locate the database file inside the folder of the application then click ok.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_angular_insert");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.php.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="" >Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Insert MySQLi Data Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-inline">
  20. <input type="text" class="form-control" ng-model="firstname" placeholder="Firstname"/>
  21. <input type="text" class="form-control" ng-model="lastname" placeholder="Lastname"/>
  22. <input type="text" class="form-control" ng-model="address" placeholder="Address"/>
  23. <br /><br />
  24. <center><button type = "button" class = "btn btn-primary" ng-click = "insetDats()" ><span class = "glyphicon glyphicon-save"></span> Insert</button></center>
  25. </div>
  26. </form>
  27. </div>
  28. <div class="col-md-8">
  29. <table class = "table table-bordered">
  30. <thead class="alert-info">
  31. <tr>
  32. <th>Firstname</th>
  33. <th>Lastname</th>
  34. <th>Address</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <tr ng-repeat = "member in members">
  39. <td>{{member.firstname}}</td>
  40. <td>{{member.lastname}}</td>
  41. <td>{{member.address}}</td>
  42. </tr>
  43. </tbody>
  44. </table>
  45. </div>
  46. </div>
  47. <script src = "js/angular.js"></script>
  48. <script src = "js/script.js"></script>
  49. </body>
  50. </html>

Creating the Main Function

This code contains the main function of the application. This code will insert a POST data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.

  1. <?php
  2. require_once 'conn.php';
  3. $data = json_decode(file_get_contents("php://input"));
  5. $firstname = $data->firstname;
  6. $lastname = $data->lastname;
  7. $address = $data->address;
  10. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  11. ?>


  1. <?php
  2. require_once 'conn.php';
  4. $data = array();
  6. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  8. while($fetch = mysqli_fetch_array($query)){
  9. $data[] = $fetch;
  10. }
  12. echo json_encode($data);
  14. ?>

Note: Make sure you save this file inside the js directory in order the script works.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http, $timeout){
  3. $http.get('data.php').then(function(response){
  4. $scope.members =;
  5. });
  7. $scope.insetDats = function(){
  8. if($scope.firstname == null || $scope.lastname == null || $scope.address == null){
  9. alert("Please complete the required field");
  10. }else{
  11. $"insert.php", {firstname: $scope.firstname, lastname: $scope.lastname, address: $scope.address})
  12. .then(function(){
  13. $scope.firstname = "";
  14. $scope.lastname = "";
  15. $scope.address = "";
  16. $scope.getData();
  17. });
  18. }
  20. }
  22. $scope.getData = function(){
  23. $http.get('data.php').then(function(response){
  24. $scope.members =;
  25. });
  26. }
  27. });

There you have it we successfully created a Simple Insert MySQLi Data using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.