<?php
/**
 * writen by Jeff Shields
 * yaadev.com
 * this file takes the result from a registration form submission
 * and submits it to a database
 * it is a formmail hook called prior to the email being sent.
 * it can be used on any mysql database
 * does not require CMS Builder
 * it is used as hook for Tectile Formmail
 * namefile: fmhookpreemail.inc.php to write to database pre email
 * rename to fmhookpostemail.inc.php to write to database post email
 */

ini_set('display_errors', 0);
error_reporting(E_ALL);

/**
 * writeToTransactionLog
 * @param  string $msg message to log
 * @return none
 */
function writeToTransactionLog($msg)
{
    global $logfile, $debug;
    $msg = date("Y-m-d H:i:s") . ":: " .$msg . "\n";
    $r = file_put_contents($logfile, $msg, FILE_APPEND);

    if ($debug) {
        // send to stdout
        echo $msg . "<br>\n";
    }
}



$logfile = ""; // path to log file
$debug = false ; // set to true to send to stdout

// database setup
$host = "";
$username = "";
$password = "";
$database = "";

// use future proof connection
$mysqli  = new mysqli($host, $username, $password, $database);
/* check connection */
if (!$mysqli) {
    writeToTransactionLog("Connect failed: ", mysqli_connect_error());
    exit(mysqli_connect_errno());
}

// get special values
// $email = $SPECIAL_VALUES['email'];

// get the cleanded values from formmail script
$email = $aCleanedValues['emailAddress'] ;
$ref  = $aCleanedValues['ref'];
$firstname = $aCleanedValues['firstname'];
$lastname = $aCleanedValues['lastname'];
$address1 = $aCleanedValues['address1'];
$address2 = $aCleanedValues['address2'];
$city  = $aCleanedValues['city'];
$province  = $aCleanedValues['province'];
$postal_code = $aCleanedValues['postal_code'];
$country = $aCleanedValues['country'];
$phone = $aCleanedValues['phone'];
$rental_location = $aCleanedValues['rental_location'];
$gender = $aCleanedValues['gender'];
$age = $aCleanedValues['age'];
$comments = $aCleanedValues['comments'];

$promo = $aCleanedValues['promo'];
$terms = $aCleanedValues['terms'];
$createdDate = date('Y-m-d H:i:s');
$updatedDate = date('Y-m-d H:i:s');

// prepare and execute the SQL it add new member to tablename
$sql = "INSERT INTO `tablename` (`firstname`, `lastname`, `email`, `transaction_number`, `address1`, `address2`, `city`, `province`, `postal_code`, `country`, `phone`,`rental_location`, `gender`, `age`, `promo`, `comments`, `terms`,`createdDate`,`updatedDate` ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

if (!($stmt = $mysqli->prepare($sql))) {
    writeToTransactionLog("Prepare failed: $ref  (" . $mysqli->errno . ") " . $mysqli->error);
}

// bind the parameters
if (!$stmt->bind_param('sssssssssssssssssss', $firstname, $lastname, $email, $ref, $address1, $address2, $city, $province, $postal_code, $country, $phone, $rental_location, $gender, $age, $promo, $comments, $terms, $createdDate, $updatedDate)) {
    writeToTransactionLog("Binding parameters failed: $ref  (" . $stmt->errno . ") " . $stmt->error);
}

//execute the statement
if (!$stmt->execute()) {
    writeToTransactionLog("Execute failed: $ref  (" . $stmt->errno . ") " . $stmt->error);
}

$stmt->close();

$mysqli->close();
