Angular CRUD Using PHP and MySQL
Angular CRUD Using PHP and MySQL
PRICE DROP
Email Subscribe
Categories
(https://fahmidasclassroom.com
programming/fram
(https://fahmidasclassroom.com
programming/frame
(https://fahmidasclassroom.com
de
This tutorial will help you to learn angular CRUD operation using PHP and MySQL. This tutorial is divided into two parts.
One for PHP & MySQL code and another for angular code.
(https://fahmidasclassroom.com
pro
PHP and MYSQL Part:
Steps:
1. Create a database named angdb. (https://fahmidasclassroom.com
programming/framewo
2. Create a table named ‘products‘ with the following fields.
(https://fahmidasclassroom.com
`name` varchar(100) NOT NULL,
values(null, 'HDD',6000),
(null, 'Monitor',8000),
(https://fahmidasclassroom.com
(null, 'Printer',10000),
(null,'Scanner',5000);
4. Create a folder named ‘ang-php-mysql‘ and create a folder named ‘api‘ under that folder.
(https://fahmidasclassroom.com
programm
(https://fahmidasclassroom.com
(https://fahmidasclassroom.com
Coursary Open
(https://fahmidasclassroom.com
index.php
programming/fram
create_product.php
update_product.php
delete_product.php
database.php
(https://fahmidasclassroom.com
6. Add the following content in database.php for database connection. programming
(https://fahmidasclassroom.com
<?php
header("Access-Control-Allow-Origin: *");
(https://fahmidasclassroom.com
header('Access-Control-Allow-Credentials: true');
(https://fahmidasclassroom.com
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('NAME', 'angdb');
(https://fahmidasclassroom.com
if ($db->connect_errno) {
}
(https://fahmidasclassroom.com
?>
pro
Courses
Free comparison tool for finding Data (https://fahmidasclassroom.com
Management courses online.
(https://fahmidasclassroom.com
Data Management
Courses
Free comparison tool for finding Data
(https://fahmidasclassroom.com
Management courses online.
W
Coursary Open (https://fahmidasclassroom.com
<?php
include 'database.php';
$products = [];
(https://fahmidasclassroom.com
$sql = "SELECT * FROM products";
if($result = $db->query($sql))
$i = 0;
(https://fahmidasclassroom.com
while($row = $result->fetch_assoc())
programming/
$products[$i]['id'] = $row['id'];
$products[$i]['name'] = $row['name'];
$products[$i]['price'] = $row['price'];
$i++;
echo json_encode($products);
else
http_response_code(404);
<?php
include 'database.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata,true);
00:00 00:00
// Validate.
return http_response_code(400);
http_response_code(201);
$product = [
echo json_encode($product);
else
http_response_code(422);
<?php
require 'database.php';
$postdata = file_get_contents('php://input');
$request = json_decode($postdata,true);
return http_response_code(400);
if($db->query($sql))
http_response_code(204);
else
return http_response_code(422);
<?php
require 'database.php';
$id = ($_GET['id'] !== null && (int)$_GET['id'] > 0)? mysqli_real_escape_string($db, (int)$_GET['id']) :
false;
if(!$id)
return http_response_code(400);
if($db->query($sql))
http_response_code(204);
else
return http_response_code(422);
Angular Part:
Steps:
1. Go to the project folder and run the following command to create a new angular project named ‘ang-crud‘. You must
press ‘y’ when the command will ask for routing module.
ng new ang-crud
If you didn’t install angular 8 before then you have to install it first before running the above command. You can follow
the tutorial given in the following link to install angular 8 with bootstrap.
https://fahmidasclassroom.com/angular-8-installation/ (https://fahmidasclassroom.com/angular-8-installation/)
2. Install and setup bootstrap for your angular project.
i
3. Add the following lines in app.module.ts to add FormsModule and HttpClientModule.
HttpClientModule
FormsModule
Remove all parts of AppComponent and add ProductsComponent in bootstrap section in app.module.ts file.
5. Run the following command from the project folder to create service for communicating with PHP codes.
6. Create a model name product.ts under src/app folder and add the following code.
id: number;
name: string;
price: number;
7. Open the src/app/api.service.ts file and import the Product model and the RxJS Observable interface.
PHP_API_SERVER = "http://localhost/ang-php-mysql/api";
readProducts(): Observable<Product[]>{
return this.httpClient.get<Product[]>(`${this.PHP_API_SERVER}/index.php`);
updateProduct(product: Product){
deleteProduct(id: number){
return this.httpClient.delete<Product>(`${this.PHP_API_SERVER}/delete_product.php/?id=${i
d}`);
10. Open the src/app/app-routing.module.ts file and add a /dashboard route for products.
];
11. Open the src/app/app.component.html file and add the following line.
<router-outlet></router-outlet>
12. open the src/app/products/products.component.ts file and import ApiService and Product.
products: Product[];
this.apiService.readProducts().subscribe((products: Product[])=>{
this.products = products;
console.log(this.products);
}) }
ngOnInit()
createOrUpdateProduct(form){
form.value.id = this.selectedProduct.id;
form.value.name = this.selectedProduct.name;
form.value.price = this.selectedProduct.price;
this.apiService.updateProduct(form.value).subscribe((product: Product)=>{
this.apiService.readProducts().subscribe((products: Product[])=>{
this.products = products;
})
});
else{
this.apiService.createProduct(form.value).subscribe((product: Product)=>{
this.apiService.readProducts().subscribe((products: Product[])=>{
this.products = products;
})
});
selectProduct(product: Product){
this.selectedProduct = product;
deleteProduct(id){
this.apiService.deleteProduct(id).subscribe((product: Product)=>{
this.apiService.readProducts().subscribe((products: Product[])=>{
this.products = products;
})
});
15. Open the src/app/products/products.component.html and add the following HTML code.
<h1>Product Management System</h1>
<form #f = "ngForm">
<label>Name</label>
<br>
<label>Amount</label>
<br>
</form>
<br/>
<div>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Operations</th>
</tr>
<td>
<button (click)="selectProduct(product)">Edit</button>
</td>
</tr>
</table>
16. Open the src/app/products/products.component.css file and add the following CSS code.
input {
width: 100%;
margin: 2px 0;
border-radius: 4px;
box-sizing: border-box;
button, input[type=button]{
background-color: #4CAF50;
border: none;
color: white;
text-decoration: none;
cursor: pointer;
#del
background-color: red;
border: none;
th, td {
padding: 1px;
text-align: left;
17. Open the index.html under src/ folder and replace the body tag with the following content.
<body>
<router-outlet></router-outlet>
</body>
"/api/*": {
"target": "http://localhost:4200",
"secure": false,
"logLevel": "debug"
"proxyConfig": "src/proxy.conf.json"
Good Luck.
Session handling using Angular 8 is given in the following tutorial.
FahmidasClassroom
15
© Copyright 2022