I am trying to save message in MySQL but failure is what i get.
public function actionChat()
{
$message = \Yii::$app->request->get('message');
$user_id = \Yii::$app->user->identity->id;
\Yii::$app->db->createCommand("INSERT INTO chat(message_content, user_id) VALUES ($message, $user_id)")->execute();
return $message;
}
When i write the same query in phpmyadmin it works fine but not in my controller. I also tried to put some string in the query like
\Yii::$app->db->createCommand("INSERT INTO chat(message_content, user_id) VALUES ('first message', $user_id)")->execute();
but result was the same. Can you give me advice where is my mistake? Thank you! AJAX:
$('body').on('click', '#chat-button', function () {
var message = $('#chat-message').val();
$.ajax({
method : 'GET',
url : '../site/chat?message=' + message,
dataType : 'text',
success : function ( data ) {
alert(data);
}
}) ;
});
GET http://letsblog/site/chat?message=fa 500 (Internal Server Error)but i think my ajax is fine. Will add it too. No other error is shown. Just it refuse to add the message in database :)