Здравствуйте, имею такую проблему. Что в результате POST запроса, выполняется GET запрос
<?php
Class Database {
private $link;
public function __construct()
{
$this->connect();
}
private function connect()
{
$dns = "mysql:host=localhost;dbname=rating";
$this->link = new PDO($dns, "root", 'root');
return $this;
}
public function execute($sql)
{
$sth = $this->link->prepare($sql);
return $sth->execute();
}
public function query($sql)
{
$sth = $this->link->prepare($sql);
$sth->execute();
$result = $sth->fetchALL(PDO::FETCH_ASSOC);
if ($result === false) {return [];}
return $result;
}
}
header('Content-Type: application/json');
$db = new Database();
$category = $_GET["category"];
if ( !empty($_GET["category"])){
$votes = $db->query("SELECT COUNT(*) as votes FROM rating WHERE category = " . $category .";");
$average_vote = $db->query("SELECT AVG(value) AS average FROM rating where category = ". $category. ";");
$votes = $votes[0]['votes'];
$avg = $average_vote[0]['average'];
print_r(json_encode(['votes' => $votes, 'average' =>$avg]));
} elseif (!empty($_POST["category"]) && !empty($_POST["value"])) {
$category = $_POST["category"];
$value = $_POST["value"];
$db->execute("INSERT INTO rating (category, value) VALUES ($category, $value);");
print_r('Success');
}