Manisha
Manisha

Reputation: 195

How to update value in database from angular

I am using angular CLI 8.1.0. I am using PHP as my backend. I have 2 buttons "approve" and "reject". When I click on the Approve button in my database the status column value should be changed from "pending" to "approved" or "rejected"(when I click on reject button). But it's updating the blank value in my database. Here I am attaching my code.

api.service.ts

updateById(id,payload)
{
   let url = `http://localhost/angular_admin/php/index.php?id=${id}`
   return this.httpClient.put(url, payload);
}

approval.component.ts

approve() {
    this.apiService.updateById(this.id, {status:'approve'})
    .subscribe((data:any)=> {
        if (data.Message == "Updated") { // check if the result is sucess then navigat to
            this.router.navigate(["/home/vendor-action"]);
        }
    });
}

index.php

<?php

    $conn=mysqli_connect("localhost","root","root","angdb");

    $request=$_SERVER['REQUEST_METHOD'];

    $data=array();
    switch($request)
    {
        case 'GET':
            response(getData());
            break;

        case 'PUT':
            response(updateData());

        default:
            #code...
            break;
    }

    function getData()
    {
        global $conn;

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="AND id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }


        $query=mysqli_query($conn,"select * from vendor where status='pending' ".$where);
        while($row=mysqli_fetch_assoc($query))
        {
            $data[]=array("id"=>$row['id'],"changeColumn"=>$row['changeColumn'],"type"=>$row['type'],"timestamp"=>$row['timestamp'],"status"=>$row['status'],"name"=>$row['name']);
        }
        return $data;
    }

    function updateData()
    {
        global $conn;
        parse_str(file_get_contents('php://input'),$_PUT);

        if(@$_GET['id'])
        {
            @$id=$_GET['id'];

            $where="where id=".$id;
        }
        else
        {
            $id=0;
            $where="";
        }

        $query=mysqli_query($conn,"update vendor set status='".$_PUT['status']."'".$where);

        if($query==true)
        {
            $data[]=array("Message"=>"Updated");
        }
        else
        {
            $data[]=array("Message"=>"Not updated");
        }
        return $data;
    }

    function response($data)
    {
        echo json_encode($data);
    }
?>

Upvotes: 0

Views: 2716

Answers (2)

Jasdeep Singh
Jasdeep Singh

Reputation: 8301

parse the data before using it anywhere with JSON.parse. Update the approve method like below:

approve() {
    this.apiService.updateById(this.id, {status:'approve'})
    .subscribe((data:any)=> {
        data = JSON.parse(data);
        if (data.Message == "Updated") { // check if the result is success then navigate
            this.router.navigate(["/home/vendor-action"]);
        }
    });
}

Can you please attach screenshot of error here.

Upvotes: 0

Ashish Mishra
Ashish Mishra

Reputation: 534

use get method in your update query of php

$query=mysqli_query($conn,"update vendor set status='".$_PUT['status']."'".$where);

Upvotes: 1

Related Questions