0

I am trying to make my rest api support post update requests the get requests work fine but I cant make the update work , I'm trying for hours :( , code should be without a framework

try {

    if (isset($_POST['title']) && isset($_GET['id'])) {
        $title = $_POST['title'];
        $text = $_POST['text'];
        $date = $_POST['date'];
        $news = $connection->prepare("UPDATE news SET title = '" . $title . "', text ='" . $text . "', date = '" . $date . "' WHERE id = " . $_GET['id']);
        $news->execute();
    }
    $result = $news;
    if ($result != 0) {
        $result = array('success' => 1);
        return $result;
        output($result);
    };
} catch (PDOException $e) {
    if (!$inProduction) {
        throw new pdoDbException($e);
    } else {
        output(['Error occured' => 'Please try again!']);
    }
}
  • 3
    Don't use `prepare()` since you are not binding anything. It is useless. – Rotimi Mar 17 '18 at 09:21
  • Agreed with @Akintunde007 and what error you are getting when you are trying this code? – Alive to die - Anant Mar 17 '18 at 09:22
  • What's the exact error message? – Nico Haase Mar 17 '18 at 09:25
  • Can you follow Example #1 in official document? https://stackoverflow.com/questions/49334400/pdo-update-error-in-code – Vuong Mar 17 '18 at 09:25
  • error 500 internal server error when trying to post data – douglas g Mar 17 '18 at 09:29
  • @douglasg are you sure there isn't any other code in the same file which can cause the 500? – Tom Udding Mar 17 '18 at 09:31
  • At `$result = $news;` - $news may be undefined if it hasn't already been in the previous `if` block. You also have a `return` followed by an `output()` which will never be executed. – Nigel Ren Mar 17 '18 at 09:35
  • `output($result);` will never be executed. You already have a return statement before that. – Rotimi Mar 17 '18 at 09:37
  • An error 500 is usually a syntax error. Enable error reporting in PHP and you will see the problem. Or take a look in the PHP logs: https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel – KIKO Software Mar 17 '18 at 09:53

0 Answers0