Tuesday, October 4, 2022
Angular 8Angular 9Bootstrap 4MysqlPhp

Angular 9 Php Mysql Database Crud Part 2 – Add User

Angular 9 Php Mysql Database Crud Part 3 - Delete User

Hello to all, welcome again on In this post, I will tell you, Angular 9 Php Mysql Database Crud Part 2 – Add User.

In this post, I am adding new user with Angular 9 to php mysql database.


You just need to replace all the files code with my below code to get proper working. For php, you need to create database into phpmyadmin name user and table name will be userdata.

Here is the part 1 post link: Angular 9 php mysql crud part 1

Here is the proper code snippet and commands and please use carefully:

1. Here are the basics commands to set angular 9 your system:

npm install -g @angular/cli 

ng new angularcrud //Create new Angular Project

$ cd angularcrud // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url


2. Here are the command to install bootstrap 4 and angular 9 new components into your angular 9 application:

npm install --save bootstrap

// add new components

ng g c header

ng g c footer

ng g c users

ng g c adduser


After run the above create component commands, you will see below folders into your Angular 9 application app folder:

angular new components
Angular 9 new components

3. Now you need to add below bootstrap files path into your angular.json file:

"styles": [
"scripts": [


4. Here is the html code, you need to add into your src\app\header\header.component.html file:

<div class="jumbotron text-center" style="margin-bottom:0">
  <h1>Angular 9 Php Mysql Database Crud Part 2</h1>
   <h3>Add User</h3>

<nav class="navbar navbar-expand-md bg-dark navbar-dark">
  <a class="navbar-brand" routerLink="/users">Home</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
    <span class="navbar-toggler-icon"></span>
  <div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" routerLink="/adduser">Add User</a>
      <li class="nav-item">
        <a class="nav-link" routerLink="/users">Users</a>


5. Here is the html code, you need to add into your src\app\footer\footer.component.html file:

<div class="jumbotron text-center" style="margin-bottom:0">


6. Now you need to add below code into your app.module.ts file:

you can just replace the complete code with below code

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { HttpClientModule } from '@angular/common/http';
import { AdduserComponent } from './adduser/adduser.component';
import { Routes, RouterModule } from '@angular/router';
import { UsersComponent } from './users/users.component';
const appRoutes: Routes = [
    { path: '', component: UsersComponent },
  { path: 'users', component: UsersComponent },
    { path: 'adduser', component: AdduserComponent }

  declarations: [
  imports: [
      { enableTracing: true } // <-- debugging purposes only
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }


7. Now you need to add below code into your app.component.html file:

<div class="container">


Start with Show users section:

1. Here is the code, you need to add your app\users\users.component.ts file:

Here I am getting data from my php mysql backend:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
  selector: 'app-users',
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.css']
export class UsersComponent implements OnInit {

  data = [];
  constructor(private http: HttpClient) {
    this.http.get('http://localhost/mypage.php').subscribe(data => {;
    }, error => console.error(error));

  ngOnInit(): void {



2. Here is the code, you need to add your app/users/users.component.html file:

Here I am showing all users data:

<table class="table table-striped">
      <tr *ngFor="let mydata of data[0]">


Start with Add user section:

1. Here is the code, you need to add into your app/adduser/app.component.ts file:

Here you see the adduser api and angular reactive form validation code also

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { FormBuilder, FormGroup, FormControl, Validators} from '@angular/forms';
import { Router } from '@angular/router';
  selector: 'app-adduser',
  templateUrl: './adduser.component.html',
  styleUrls: ['./adduser.component.css']
export class AdduserComponent implements OnInit {
  registerForm: FormGroup;
  submitted = false;
  constructor(private http: HttpClient, private formBuilder: FormBuilder, private router: Router) { }
  get f() { return this.registerForm.controls; }
  onSubmit() {
  this.submitted = true;
        // stop here if form is invalid
        if (this.registerForm.invalid) {
    // Initialize Params Object
    var myFormData = new FormData();
    const headers = new HttpHeaders();
    // Begin assigning parameters
      , myFormData).subscribe((res: Response) => {
  ngOnInit(): void {
  this.registerForm ={
            name: ['', Validators.required],
            phone: ['', Validators.required],
            email: ['', [Validators.required,]]



2. Here is the code, you need to add into app/adduser/app.component.html file:

Here is the code for add user form and also angular 9 reactive form validation code:

<h2 class="mt-3 mb-3">Add User</h2>
<form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
  <div class="form-group">
    <label for="name">Name:</label>
    <input type="text" class="form-control" placeholder="Enter name" id="name" formControlName="name" [ngClass]="{ 'is-invalid': submitted && }">
  <div *ngIf="submitted &&" class="invalid-feedback">
      <div *ngIf="">Name is required</div>
  <div class="form-group">
    <label for="pwd">Email:</label>
    <input type="email" class="form-control" placeholder="Enter email" id="email" formControlName="email" [ngClass]="{ 'is-invalid': submitted && }">
  <div *ngIf="submitted &&" class="invalid-feedback">
    <div *ngIf="">Email is required</div>
      <div *ngIf="">Email must be a valid email address</div>
  <div class="form-group">
    <label for="phone">Phone:</label>
    <input type="text" class="form-control" placeholder="Enter phone" id="phone" formControlName="phone" [ngClass]="{ 'is-invalid': submitted && }">
  <div *ngIf="submitted &&" class="invalid-feedback">
      <div *ngIf="">Phone is required</div>
  <button type="submit" class="btn btn-primary mb-3">Submit</button>


Here is the php code myphp.php for add and get user data and also please xampp control panel and this file will me in htdocs folder:

1. Code for php file(mypage.php):

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$conn = new mysqli('localhost','root','','user');
        // Add User
  $sql = "INSERT INTO userdata (firstname, email, mobile)
  VALUES ('".$_POST['name']."', '".$_POST['email']."', ".$_POST['phone'].")";

  if ($conn->query($sql) === TRUE) {
    $myJSON = json_encode("New user created successfully");
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
// Get User
$sql = "SELECT * FROM userdata";
$result = $conn->query($sql);
$myArr = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$myArr[] = $row;
} else {
echo "0 results";

$myJSON = json_encode($myArr);
echo $myJSON;


This is it and please run ng serve and check the proper process. If you have any kind of query then please comment below.


Thank you

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Angular12, Angular 13, Angular 14, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.