請問一下create方法報錯這是怎么回事
??
Fatal error: Uncaught exception 'Exception' with message '發(fā)布文章失敗' in C:\phpStudy\PHPTutorial\WWW\article\lib\Article.php:47 Stack trace: #0 C:\phpStudy\PHPTutorial\WWW\article\index.php(15): Article->create('\xE6\x96\x87\xE7\xAB\xA0\xE6\xA0\x87\xE9\xA2\x981', '\xE6\x96\x87\xE7\xAB\xA0\xE5\x86\x85\xE5\xAE\xB91', 1) #1 {main} thrown in?C:\phpStudy\PHPTutorial\WWW\article\lib\Article.php?on line?47
2017-11-07
index.php
require __DIR__.'/lib/Article.php';
$pdo=new PDO('mysql:host=localhost;dbname=res','root','root');
$article = new article($pdo);
var_dump($article->create('文章標題1','文章內(nèi)容1',1))
Article.php
require_once __DIR__."/ErrorCode.php";
class Article{
private $_db;
public function __construct($_db){
? ?$this->_db = ?$_db;
}
public function create($title,$content,$userId){
? ?if(empty($title)){
? ? ? ?throw new Exception("文章標題不能為空", ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
? ?}
? ?if(empty($content)){
? ? ? ?throw new Exception("文章內(nèi)容不能為空", ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
? ?}
? ?$addtime = time();
? ?//數(shù)據(jù)寫入
? ?$sql = 'INSERT INTO `article` (`title`,`content`,`addtime`,`user_id`) VALUES(:title,:content,:addtime,:user_id)';
? ?$stmt = $this->_db->prepare($sql);
? ?$stmt->bindParam(':title',$title);
? ?$stmt->bindparam(':content',$content);
? ?$stmt->bindparam(':addtime',$addtime);
? ?$stmt->bindparam(':user_id',$userId);
? ?if(!$stmt->execute()){
? ? ? ?throw new Exception("發(fā)布文章失敗", ErrorCode::ARTICLE_CREATE_FAIL);
? ?}
? ?return [
? ? ? ?'articleId' => $this->_db->lastInsertId(),
? ? ? ?'title' ? ?=> $title,
? ? ? ?'content' ?=> $content,
? ? ? ?'addtime' ?=> $addtime,
? ? ? ?'userId' ? => $userId
? ?];
}
2017-11-07
貼代碼朋友,不貼大家都只能猜,最好帶上請求參數(shù)