<?php

include 'config_transactions.php';
$conn = mysql_connect($server, $user, $pass);
mysql_select_db($db);
$p_time = timeReadable2DB($_POST['time'], $_POST['pm']);
$p_date = dateReadable2DB($_POST['date']);
$client_last = $_POST['last'];
$client_first = $_POST['first'];
$tutor_last = $_POST['tutorlast'];
$tutor_first = $_POST['tutorfirst'];
$tutor_found = 0;
$client_found = 0;
$tutor_query = "SELECT tid FROM Tutor WHERE First='{$tutor_first}' AND Last='{$tutor_last}'";
$client_query = "SELECT cid FROM Client WHERE First='{$client_first}' AND Last='{$client_last}'";
$client_ret = mysql_query($client_query, $conn);
if (mysql_num_rows($client_ret) == 0) {
    echo "Error: Client not found in database.";
} else {
    $client = mysql_fetch_assoc($client_ret);
    $cid = $client[cid];
    $client_found = 1;
}
$tutor_ret = mysql_query($tutor_query, $conn);
if (mysql_num_rows($tutor_ret) == 0) {
    echo "Error: Tutor not found in database.";
} else {
    $tutor = mysql_fetch_assoc($tutor_ret);
    $tid = $tutor[tid];
    $tutor_found = 1;
}
 $p_charged = $_POST['charged'];
 $p_payTutor = $_POST['payTutor'];
 $p_requested = $_POST['requested'];
 $p_status = $_POST['status'];
 $p_paidTutor = $_POST['paidtutor'];
 $p_received = $_POST['received'];
 $p_billing = $_POST['billingcycle'];
 if (!isset($p_tid)) {
     $p_tid = 0;
 }
 if (!isset($p_time) || trim($p_time) == '') {
     $timeSet = 0;
     $submitted = 0;
 } else {
     if (preg_match("/^([0-9]|1[012]):[0-5][0-9]\$/", trim($p_time))) {
         $p_time = timeReadable2DB($p_time, $p_ampm);
     } else {
         $timeSet = 0;
         $submitted = 0;
     }
 }
 if (!isset($p_date) || trim($p_date) == '') {
     $dateSet = 0;
     $submitted = 0;
 } else {
     if (preg_match("/^[0-9]{2}\\/[0-9]{2}\\/[0-9]{2}\$/", trim($p_date))) {
         $p_date = dateReadable2DB($p_date);
     } else {
         $dateSet = 0;
         $submitted = 0;
     }