環境
MacOS Mojave 10.14.5
VirtualBox 6.0.0
Vagrant 2.2.3
CentOS Linux release 7.6.1810
Apache/2.4.6
PHP 7.2.19
mysql5.7.26
パーフェクトPHPというPHPの参考書を見ながらブログシステムの構築を行っているのですが、以下のエラーの原因が突き止められません。
Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null in /var/www/mini-blog.com/application/core/DbRepository.php:20
アカウント登録機能を実装し、投稿処理を作成している際にエラーが起こりました。
ログインしているユーザーのホームページにあたるindexアクションを作成し、投稿処理にあたるpostアクションを作成しました。
Postメソッドの実行時、statusテーブルから情報を取得する部分にてエラーが起こっているのではないかと想定しているのですが、解決できていないため質問させていただきます。
該当されると思われるファイルのソースコードを以下に記載いたします。
core/DbRepository.php
public function execute($sql,$params = array()) { $stmt = $this->con->prepare($sql); $stmt->execute($params); return $stmt; }
models/StatusRepository.php
public function insert($user_id, $body) { $now = new DateTime(); $sql = " INSERT INTO status(user_id, body, created_at) VALUES(:user_id, :body, :created_at) "; $stmt = $this->execute($sql, array( ':user_id' => $user_id, ':body' => $body, ':created_at' => $now->format('Y-m-d H:i:s'), )); }
controllers/StatusController.php
public function postAction() { if(!$this->request->isPost()){ $this->forward404(); } $token = $this->request->getPost('_token'); if(!$this->checkCsrfToken('status/post', $token)){ return $this->redirect('/'); } $body = $this->request->getPost('body'); $errors = array(); if(!strlen($body)){ $errors[] = 'ひとことを入力して下さい'; }else if(mb_strlen(($body) > 200)){ $errors[] = 'ひとことは200文字以内で入力して下さい'; } if(count($errors) === 0){ $user = $this->session->get('user'); $this->db_manager->get('Status')->insert($user['id'],$body); return $this->redirect('/'); } $user = $this->session->get('user'); $statuses = $this->db_manager->get('Status') ->fetchAllPersonalArchivesByUserID($user['id']); return $this->render(array( 'errors' => $errors, 'body' => $body, 'statuses' => $statuses, '_token' => $this->generateCsrfToken('status/post'), ),'index'); }
宜しくお願いいたします。
回答2件
あなたの回答
tips
プレビュー