What am I doing wrong with the php://input function?

Viewed 75

My current problem is that I have to create a backend API connected to a MySQL database. My classmate found a tutorial on how to create one but it uses something I am not familiar with, nor can I find a lot of documentation.

I would like to know about how this works exactly, I am going to include a block of code from the tutorial that makes use of it. I hope that you can help me out.

So far I've tried googling (no real results) and asking my teachers but they do not seem to have any answer to help me out. From what I've discovered it is sort of a JSON string in terms of reading/output.

Function to delete a row from the database, as you can see it uses what seems to be a JSON API call. (p.Categorie_ID)

  public function delete(){
        $query = "DELETE FROM categorie WHERE Categorie_ID= p.Categorie_ID";

        $stmt = $this->connection->prepare($query);

        $stmt->execute();

        return $stmt;
    }
$p  = array(
      "Object_ID" => $Object_ID,
      "Object_naam" => $Object_naam,
      "Object_merk" => $Object_merk,
      "Object_type" => $Object_type,
      "Object_status" => $Object_status,
      "Categorie_ID" => $Categorie_ID
    );
    array_push($Objects["body"], $p);
  }

Above I posted what I think to be the "p" variable in the original code block.

<br />
<b>Notice</b>:  Trying to get property of non-object in <b>C:\xampp\htdocs\PHP\PHP API_Categories\entities\Categories\delete.php</b> on line <b>21</b><br />
object(Categories)#3 (4) {
  ["connection":"Categories":private]=>
  object(PDO)#1 (0) {
  }
  ["table_name":"Categories":private]=>
  string(10) "Categories"
  ["Categorie_ID"]=>
  NULL
  ["Categorie_naam"]=>
  NULL
}
NULL
{"message": "category was deleted."}

This is what it outputs when I run/open the file. It does give the message when it is deleted; yet all data is returned as NULL. What am I overlooking here?

I expected the function to delete the table mentioned (would have to look into which) but it doesn't do or return anything at all while it should be doing so.

(Note: The full source code can be found here if you wish to read over what I was trying to do originally: https://www.techiediaries.com/php-rest-api/)

1 Answers

You are reading the data from php://input correctly. The problem is with your SQL query. To pass the parameter from your $p array you need to insert parameter into the query and bind the data. Also you need to send the array as an argument to the function.

// this function takes in a parameter $p 
public function delete(string $Categorie_ID) {
    $query = "DELETE FROM categorie WHERE Categorie_ID = ?";
    $stmt = $this->connection->prepare($query);
    // Pass an element from $p to the execute function to replace the ? placeholder
    $stmt->execute([
        $Categorie_ID
    ]);

    // return $stmt; // there is nothing to return from delete action
}

You can then call the function like this:

$yourObj->delete($p['Categorie_ID']); // pass the category from the array as an argument
Related