<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$sql = null;
//$con = mysqli_connect("localhost", "root", "R3M0T31", "cropmonitor");
$con = mysqli_connect("localhost", "cropmonitor", "brvnCcaEYxlPCS3", "cropmonitor");

//set todays date
$date = date("Y-m-d H:i:s");

//Get figures
$email = (isset($_POST["email"])) ? $_POST["email"] : "";
$client_name = (isset($_POST["name"])) ? $_POST["name"] : "";
$site_address = (isset($_POST["site_address"])) ? $_POST["site_address"] : "";
$state_postcode = (isset($_POST["state_postcode"])) ? $_POST["state_postcode"] : "";
$analysis_type = (isset($_POST["analysis_type"])) ? $_POST["analysis_type"] : "";
$lab_no = (isset($_POST["lab_no"])) ? $_POST["lab_no"] : "";
$batch_no = (isset($_POST["batch_no"])) ? $_POST["batch_no"] : "";
$date_sampled = (isset($_POST["date_sampled"])) ? $_POST["date_sampled"] : "";
$sample_id = (isset($_POST["sample_id"])) ? $_POST["sample_id"] : "";
$site_id = (isset($_POST["site_id"])) ? $_POST["site_id"] : "";
$crop_type = (isset($_POST["crop_type"])) ? $_POST["crop_type"] : "";
$n = (isset($_POST["n"])) ? $_POST["n"] : "";
$p = (isset($_POST["p"])) ? $_POST["p"] : "";
$k = (isset($_POST["k"])) ? $_POST["k"] : "";
$s = (isset($_POST["s"])) ? $_POST["s"] : "";
$mg = (isset($_POST["mg"])) ? $_POST["mg"] : "";
$ca = (isset($_POST["ca"])) ? $_POST["ca"] : "";
$na = (isset($_POST["na"])) ? $_POST["na"] : "";
$fe = (isset($_POST["fe"])) ? $_POST["fe"] : "";
$mn = (isset($_POST["mn"])) ? $_POST["mn"] : "";
$zn = (isset($_POST["zn"])) ? $_POST["zn"] : "";
$cu = (isset($_POST["cu"])) ? $_POST["cu"] : "";
$b = (isset($_POST["b"])) ? $_POST["b"] : "";
$m = (isset($_POST["m"])) ? $_POST["m"] : "";
$co = (isset($_POST["co"])) ? $_POST["co"] : "";
$se = (isset($_POST["se"])) ? $_POST["se"] : "";
$ch = (isset($_POST["cl"])) ? $_POST["cl"] : "";
// calculate meq/100g for additional calulations
$k_meq = ( $k / 390 ) ;
$s_meq = ( $s / 100000 * 33333 * 1.11 ) ;
$ca_meq = ( $ca / 200 ) ;
$mg_meq = ( $mg / 120 ) ;
$na_meq = ( $na / 230 );
$cl_meq = ( $cl / 100000 * 448.34 * 1.11 ) ;

//$rand = substr(md5(microtime()),rand(0,26),5);
$rand = mt_rand(10000, 99999);

// Check connection
	if (mysqli_connect_errno())
		{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
		
$sql = mysqli_query($con, "INSERT into `plant_records`
            (
                date,
                email,
                client_name,
                site_address,
                state_postcode,
                analysis_type,
                lab_no,
                batch_no,
                date_sampled,
                sample_id,
                site_id,
                crop_type,
                n,
                p,
                k,
                s,
                mg,
                ca,
                na,
                fe,
                mn,
                zn,
                cu,
                b,
                m,
                co,
                se,
                cl,
                k_meq,
                s_meq,
                ca_meq,
                mg_meq,
                na_meq,
                rand
            ) VALUES (
                '" . $date . "',
                '" . $email . "',
                '" . $client_name . "',
                '" . $site_address . "',
                '" . $state_postcode . "',
                '" . $analysis_type . "',
                '" . $lab_no . "',
                '" . $batch_no . "',
                '" . $date_sampled . "',
                '" . $sample_id . "',
                '" . $site_id . "',
                '" . $crop_type . "',
                '" . $n . "',
                '" . $p . "',
                '" . $k . "',
                '" . $s . "',
                '" . $mg . "',
                '" . $ca . "',
                '" . $na . "',
                '" . $fe . "',
                '" . $mn . "',
                '" . $zn . "',
                '" . $cu . "',
                '" . $b . "',
                IF('" . $m . "'='',NULL,'" . $m . "'),
                IF('" . $co . "'='',NULL,'" . $co . "'),
                IF('" . $se . "'='',NULL,'" . $se . "'),
                IF('" . $cl . "'='',NULL,'" . $cl . "'),
                '" . $k_meq . "',
                '" . $s_meq . "',
                '" . $ca_meq . "',
                '" . $mg_meq . "',
                '" . $na_meq . "',
                '" . $rand . "'
            )" );
$insert_id = mysqli_insert_id($con);
            
if ($sql === TRUE)
	{
	//echo "success"; //CHECKING
	// forward to results page if successfully inserts to database
	echo "<script>location.href = '[[~26]]?rand=" . $rand . "&cid=" . $sample_id . "&rid=" . $insert_id . "&stid=" .$crop_type . "';</script>";
    } else {
    die(mysqli_error($con)); // TODO: better error handling
    //echo "User Profile incorrect";
}
/.