public function doPost() { try { $id = I('id'); $title = I('title'); $content = I('content'); $data = array('title' => $title, 'content' => $content); $model = new PostModel(); if ($model->where(array('postId' => $id))->save($data) === false) { throw new Exception('编辑失败'); } $this->success('编辑成功', U('post/index')); } catch (Exception $e) { $this->error($e->getMessage()); } }
/** * 删除帖子 */ public function postDelete() { $this->checkLogin(); $id = I('id'); $model = new PostModel(); $post = $model->find($id); if (empty($post)) { $this->error('帖子不存在'); } if ($post['userId'] != $this->user['userId']) { $this->error('你无权删除'); } $result = $model->delete($id); if ($result === false) { $this->error('删除失败'); } else { $callback = I('callback'); if (empty($callback)) { $callback = U('posts'); } $this->success('删除成功', $callback); } }