<?php
require_once('config/config.php');
require_once('foo/functions.php');
session_start();

if(isset($_POST['submit']))
{

if ($_POST['msg_subject'] == ""){ redirect('messages.php?error=' . urlencode('Enter a subject for your messages'));}
elseif ($_POST['msg_cnt'] == ""){ redirect('messages.php?error=' . urlencode('Your message cannot be empty'));}
else{
$msg_subject = $_POST['msg_subject'];
$msg_cnt = $_POST['msg_cnt'];
$msg_sender = $_POST['msg_sender'];
$msg_rcvr = $_POST['msg_rcvr'];
$msg_read = $_POST['msg_read'];
$adm_read = $_POST['adm_read'];
$sender_name = $_POST['sender_name'];

dbconnect();
$stmt = $pdo->prepare('INSERT INTO '.MSGS_TABLE_NAME.' (msg_subject, msg_cnt, msg_sender, msg_rcvr, msg_date, msg_read, adm_read, sender_name) VALUES (:subject, :cnt, :sender, :rcvr, NOW(), :msgread, :admread, :name)');

$stmt->execute(array(':subject' => $msg_subject, ':cnt' => $msg_cnt, ':sender' => $msg_sender, ':rcvr' => $msg_rcvr, ':msgread' => $msg_read, ':admread' => $adm_read, ':name' => $sender_name));
if ($pdo->lastInsertId())

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_SESSION['user_acc'], 'Message Sent Successfully');
redirect('messages.php?msg=' . urlencode('Message Successfully Sent'));		
}
}

?>