PHP Illegal string offset – Angular Post Request

  angular, php

I’m working with Angular post request and PHP. None of the other questions helped me.

I get the warnings:

Warning: Illegal string offset ‘access’ … on line 19

Warning: Illegal string offset ‘zwrot_id’… on line 20

PHP Code:

require_once("../connections/connection.php");


if($json = file_get_contents("php://input")){

$data = (array)json_decode($json,true);

if(isset($data['zwrot_id']) && isset($data['info']) && isset($data['person']) && isset($data['printer']) && isset($data['data_zwrotu']) && isset($data['ink']) && isset($data['access'])){

      $person = $data['person'];
      $printer = $data['printer'];
      $ink = $data['ink'];
      $data = $data['data_zwrotu'];
      $access = $data['access']; -- Warning
      $id = $data['zwrot_id']; -- Warning

        if($access==2){

        $sql = sprintf(
            "UPDATE zwrot SET %s %s %s %s %s WHERE id=:zwrot_id",
        is_numeric($person) ? "osoba_id=:person_id," : null,
        is_numeric($printer) ? "drukarka_id=:printer_id," : null,
        is_numeric($ink) ? "tusz_toner_id=:ink_id," : null,
        "data_zwrotu=:date,",
        "zwrot_info=:info");
        
        try{ 
            $stmt = $db_connection->prepare($sql);
         ...

Angular Code:

    editReturn(data: any): Observable<any>{
     
      const base = this.http.post(`${this.baseUrl}/update_zwrot`,{
        zwrot_id: data.id,
        person: data.person,
        printer: data.printer,
        ink: data.ink,
        data_zwrotu: data.datetime,
        info: data.info,
        access: this.verify.user.access
     });

     const request = base.pipe(
      map((data: string)  => {
        if(data=="Sukces"){
        this.alert.next("EDYCJA DOKONANA");
          return "Sukces";
        }
        return this.checkData(data);
      })
    )

  return request;
 }

I checked if the decoded array contains the values and it does

{"zwrot_id":"1","person":"5","printer":"Canon PIXMA","ink":"HP 650 Czarny","data_zwrotu":"2020-11-28","info":"","access":"2"}

Source: Ask PHP

LEAVE A COMMENT