简体   繁体   English

我在php://输入函数中做错了什么?

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

My current problem is that I have to create a backend API connected to a MySQL database. 我目前的问题是我必须创建一个连接到MySQL数据库的后端API。 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. 根据我的发现,它在读/输出方面是一种JSON字符串。

Function to delete a row from the database, as you can see it uses what seems to be a JSON API call. 从数据库中删除行的功能,因为您可以看到它使用似乎是JSON API调用。 (p.Categorie_ID) (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. 上面我发布了我认为原始代码块中的“p”变量。

<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. 但所有数据都返回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/ ) (注意:如果您希望阅读我最初尝试做的事情,可以在此处找到完整的源代码: https//www.techiediaries.com/php-rest-api/

You are reading the data from php://input correctly. 您正在从php://input正确读取数据。 The problem is with your SQL query. 问题出在您的SQL查询中。 To pass the parameter from your $p array you need to insert parameter into the query and bind the data. 要从$p数组传递参数,您需要将参数插入查询并绑定数据。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM