第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

請問一下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

正在回答

2 回答

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
? ?];
}

0 回復(fù) 有任何疑惑可以回復(fù)我~

貼代碼朋友,不貼大家都只能猜,最好帶上請求參數(shù)

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

請問一下create方法報錯這是怎么回事

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號