0

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);
    }
?>
4
  • print the data before sending it. To check is value is there or not Commented Mar 12, 2020 at 5:45
  • @SohailAhmad getting error on console SyntaxError: Unexpected token < in JSON at position 0 Commented Mar 12, 2020 at 5:49
  • There is a syntax error, debug code line by line to check the error Commented Mar 12, 2020 at 6:03
  • @SohailAhmad actully i am new in this field. I dont know how to debug and identify the error line by line :( Commented Mar 12, 2020 at 6:10

2 Answers 2

1

use get method in your update query of php

$query=mysqli_query($conn,"update vendor set status='".$_PUT['status']."'".$where);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.