瀏覽代碼

Insert into database with not working error catch

Namespaces
Ruben De Baets 2 年之前
父節點
當前提交
4cdb6d8b5d
共有 1 個文件被更改,包括 15 次插入2 次删除
  1. 15
    2
      public/index.php

+ 15
- 2
public/index.php 查看文件

@@ -70,6 +70,8 @@ $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response
if (isset($_SESSION['username'])) {
if ($request->getMethod() == 'GET') {
addNavbar($response);
$response->getBody()->write($_SESSION['error']);
$response->getBody()->write('<hr/>');
$response->getBody()->write('<form action="/blog/create" method="POST">');
$response->getBody()->write('<label for="slug">slug:</label>');
$response->getBody()->write('<input type="text" name="slug"/><br/>');
@@ -79,10 +81,21 @@ $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response
$response->getBody()->write('<textarea type="textarea" name="content"></textarea><br/>');
$response->getBody()->write('<input type="submit"/>');
addFooter($response);
unset($_SESSION['error']);
} else {
$data = $request->getParsedBody();
$_SESSION['blogs'][] = ["slug" => $data['slug'], "title" => $data['title'], "content" => "Lorem ipsum 4"];
return $response->withHeader('Location', '/')->withStatus(302);
global $db;
$sql = "INSERT INTO blogs (slug, title, content) VALUES ('" . $data['slug'] . "', '" . $data['title'] . "', '" . $data['content'] . "')";
$ret = $db->exec($sql);
//$_SESSION['blogs'][] = ["slug" => $data['slug'], "title" => $data['title'], "content" => "Lorem ipsum 4"];

if ($ret) {
return $response->withHeader('Location', '/')->withStatus(302);
} else {
$err = $db->lastErrorMsg();
$_SESSION['error'] = $err;
return $response->withHeader('Location', '/blog/create')->withStatus(302);
}
}
} else {
addNavbar($response);

Loading…
取消
儲存