<?php 
	require_once("functions.php");
	require_once("db-const.php");

## connect mysql server
	$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
	# check connection
	if ($mysqli->connect_errno) {
		echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
		exit();
	}
## query database
	# prepare data for insertion
	$fname	= $mysqli->real_escape_string($_POST['fname']);
	$lname	= $mysqli->real_escape_string($_POST['lname']);
	$email	= $mysqli->real_escape_string($_POST['email']);
	$phone	= $mysqli->real_escape_string($_POST['phone']);
	$message = $mysqli->real_escape_string($_POST['message']);
    $date = date("Y-m-d H:i:s");



    # insert data into mysql database
    $sql = "INSERT  INTO `messages` (`fname`, `lname`, `email`, `phone`, `message`, `date`, `isRead`) 
            VALUES ('{$fname}', '{$lname}', '{$email}', '{$phone}', '{$message}', '{$date}',0)";

    if ($mysqli->query($sql)) {
        redirect_to("../contact.php?msg=sent");
    } else {
        echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
        exit();
    }