diff --git a/connection.php b/connection.php index 37c9e4d..7d38394 100644 --- a/connection.php +++ b/connection.php @@ -1,32 +1,18 @@ $first_name, + 'last_name' => $last_name, + 'author' => $author, + 'content' => $content + ); + $post = Post::add($arr); + } + + public function delete() + { + if (isset($_POST['id'])) + $post = Post::delete($_POST['id']); + require_once('views/posts/delete.php'); + } + + public function update() + { + require_once('views/posts/update.php'); + } + public function doUpdate() + { + if (isset($_POST['id'])) + $id = $_POST['id']; + $first_name = $_POST['first_name']; + $last_name = $_POST['last_name']; + $author = $_POST['author']; + $content = $_POST['content']; + $arr = array( + 'id' => $id, + 'first_name' => $first_name, + 'last_name' => $last_name, + 'author' => $author, + 'content' => $content + ); + $post = Post::update($array); + } } + ?> \ No newline at end of file diff --git a/models/post.php b/models/post.php index 5cd60b8..adbc1a4 100644 --- a/models/post.php +++ b/models/post.php @@ -1,23 +1,18 @@ author directly public $id; + public $first_name; + public $last_name; public $author; public $content; + public $created; + public $modified; - /** - * Post constructor. - * @param $id - * @param $author - * @param $content - */ public function __construct($id, $author, $content) { - $this->id = $id; + $this->id = $id; $this->author = $author; $this->content = $content; } @@ -53,5 +48,52 @@ public static function find($id) { return new Post($post['id'], $post['author'], $post['content']); } - } + public static function add($arr) { + $db = Db::getInstance(); + $time = date("Y-m-d h:sa:i"); + $sql = "INSERT INTO `posts` (`id`, `first_name`, `last_name`, `author`, `content`, `created`, `modified`) VALUES (NULL, '".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['author']."', '".$_POST['content']."', '".$time."', '".$time."')"; + + $query = $db->query($sql); + if($query) + { + echo "Successful!"; + } + else { + echo "Error!"; + } + } + + public static function delete($id) { + try + { + $db = Db::getInstance(); + // we make sure $id is an integer + $id = intval($id); + $sql = "DELETE FROM `posts` WHERE id= $id"; + $query = $db->exec($sql); + echo "Successful!"; + } + catch(PDOException $e) + { + echo $query . "
" . $e->getMessage(); + } + } + + public static function update($array) { + try + { + $db = Db::getInstance(); + // we make sure $id is an integer + $id = $_POST['id']; + $id = intval($id); + $sql = "UPDATE `posts` SET `first_name`='".$_POST['first_name']."','".`last_name`."'='".$_POST['last_name']."','".`author`."'='".$_POST['author']."','".`content`."'='".$_POST['content']."' WHERE id = $id "; + $query = $db->query($sql); + echo "Successful!"; + } + catch(PDOException $e) + { + echo $query . "
" . $e->getMessage(); + } + } + } ?> \ No newline at end of file diff --git a/post.sql b/post.sql index 1a616aa..4302ccf 100644 --- a/post.sql +++ b/post.sql @@ -1,4 +1,3 @@ -------------------------------------------------------- DROP TABLE IF EXISTS `posts`; diff --git a/routes.php b/routes.php index d0b5ab5..823603e 100644 --- a/routes.php +++ b/routes.php @@ -18,7 +18,7 @@ function call($controller, $action) { // we're adding an entry for the new controller and its actions $controllers = array('pages' => ['home', 'error'], - 'posts' => ['index', 'show']); + 'posts' => ['index', 'show', 'add','doAdd', 'delete', 'update', 'doUpdate']); /** @var string $controller */ if (array_key_exists($controller, $controllers)) { diff --git a/views/layout.php b/views/layout.php index 279fb4f..c4fed09 100644 --- a/views/layout.php +++ b/views/layout.php @@ -4,8 +4,11 @@
- Home + Home Posts + Add + Delete + Update
diff --git a/views/posts/add.php b/views/posts/add.php new file mode 100644 index 0000000..d4659f2 --- /dev/null +++ b/views/posts/add.php @@ -0,0 +1,28 @@ + + + + Them du lieu + + +

+
+ + + + + + + + + + + + + + + +
first_name:
last_name:
author:
content:
+ +
+ + \ No newline at end of file diff --git a/views/posts/delete.php b/views/posts/delete.php new file mode 100644 index 0000000..87c3ecc --- /dev/null +++ b/views/posts/delete.php @@ -0,0 +1,12 @@ + + + + Xoa du lieu + + +

+
+ ID: + + + \ No newline at end of file diff --git a/views/posts/update.php b/views/posts/update.php new file mode 100644 index 0000000..e913dca --- /dev/null +++ b/views/posts/update.php @@ -0,0 +1,30 @@ + + + + Chinh sua du lieu + + +

+ + + + + + + + + + + + + + + + +
first_name:
last_name:
author:
content:
+

+ ID: + +
+ + \ No newline at end of file