Chat形式のメッセージ内容をDBに保存するため下記のPHPを記載しています。テーブルtbl_messages
にarrayの内容をinsertしたいのですが、実行してもDBに保存されません。
色々試したところ、description以外はDBに保存できる様ですが、descriptionだけnullになってしまう感じが、問題のようです。(descriptionはtextareaでメッセージを入力する部分です)
HTML側も下記に記載しておりますが、問題点がお分かりかたはご指摘いただけると幸いです。
PHP
1public function sendMessage(){ 2 3 extract($this -> reqData); 4 $desc = (isset($description) && trim($description) != null) ? trim($description) : null; 5 if ($desc != null) { 6 7 $lastId = $this -> db -> insert('tbl_messages', 8 array( 9 'senderId' => $this->sessUserId, 10 'receiverId' => ($this->sessUserId == $this->$receiverID['provider']['userId']) ? $this->$receiverID['provider']['userId'] : $this->$receiverID['userId'], 11 'type' => 'workroom', 12 'subject' => $this->proj['title'] . " - Workroom conversation", 13 'description' => $desc, 14 'createdDate' => date('Y-m-d H:i:s'), 15 'projectId' => $this->proj['id'], 16 ))->lastInsertId(); 17 18 19 if ($lastInsertId > 0) { 20 21 if ($this->dataOnly) { 22 return array( 23 'status' => 1, 24 'msg' => 'Success', 25 26 ); 27 } else { 28 echo json_encode(array( 29 'status' => 1, 30 'msg' => 'undefined', 31 32 )); 33 } 34 } 35 } 36 37 return $html; 38 }
HTML
1 2<form class="chat-form" id="sendMsg"> 3 <div class="form-group"> 4 <textarea name="message" class="form-control" rows="4" id="description" 5 name="description" 6 placeholder="メッセージを入力..." 7 data-validation="validateDescription" 8 data-validation-error-msg="Please check description carefully."></textarea> 9 </div> 10 <div class="form-group text-right"> 11 <input type="hidden" name="action" value="method"/> 12 <input type="hidden" name="method" value="sendMessage"/> 13 <button type="submit" class="btn btn-link" data-ele="sendMessage"> 14 送信 15 </button> 16 </div> 17 </form> 18
フレームワーク等使っているのであればそちらも環境情報として提示することで回答を得やすくなりますよ(タグもご利用ください)処理自体はどこまで通っているのでしょうか?エラーが出ていないか、想定通りのデータが渡ってきているかご確認のうえ、質問に追記してください。
アドバイスありがとうございます。フレームワークは使っていないです。エラーも出ておらず、ブラウザ上は{"status":1,"msg":"undefined","html":null} となっていますのでうまくDBにinsertされていないのではと感じています。
自作クラスの一部を切り出して見せられても何をしているのかさっぱりわからないので答えられません。今回の不具合を再現するのに最低限必要なコードを抜き出して提示すると回答が得られやすいと思いますし、その過程で原因に気づくことも多々あります。
こちらアドバイスありがとうございます。Arrayの内容のうち、descriptionがDB保存時にnullになってしまいます。HTMLのコードも追記いたしました。descriptionはtextareaの部分を参照しております。もしお分かりでしたら、宜しくお願い致します。
確認ですが、そもそもdbという変数はなんのオブジェクトですか?PDO?PDOにinsertなんてメソッド無いような…
回答2件
あなたの回答
tips
プレビュー