<?php

// Set global variables to easier names
$uname = $_POST["name"];
$umail = $_POST["mail"];
$ut1 = $_POST["trait1"];
$ut2 = $_POST["trait2"];
$ut3 = $_POST["trait3"];
$ut4 = $_POST["trait4"];
$ut5 = $_POST["trait5"];
$ut6 = $_POST["trait6"];
$date = date("Y-m-d"); 


// Set Mysql Variables
$host = "localhost";
$user = "nlit1bde_graham";
$pass = "crystal1974";
$db = "nlit1bde_self";
$table = "example";

// Connect to Mysql, select the correct database, and run the query which adds the data gathered from the form into the database

$conn = mysqli_connect($host, $user, $pass, $db);

if (mysqli_connect_errno()) {
    die("<h1>Connection failed: " . mysqli_connect_error() . "</h1>");

}

// Prepare SQL query using prepared statements to prevent SQL injection
$query = "INSERT INTO $table (exec,name,mail,st,pr,fo,pe,re,org) 
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($conn, $query);

if ($stmt) {
    // Bind parameters
    mysqli_stmt_bind_param($stmt, 'sssssssss', $date, $uname, $umail, $ut1, $ut2, $ut3, $ut4, $ut5, $ut6);

    // Execute statement
    if (mysqli_stmt_execute($stmt)) {
        echo "<h1>Data inserted successfully</h1>";
		
	


$url = "http://www.selfmiracle.com/downloadysm6aiy40p6.html";
header ("Location: $url");

$message="$uname the link to download session 6 of Your Self Miracle and complete the actions to receive session 7 is:\n\nhttp://www.selfmiracle.com/downloadysm6aiy40p6.html\n\n\nRegards\n\n\n\nSledge\nhttp://www.selfmiracle.com";
if(mail($umail,"Your Self Miracle Session 6",$message,"From: ysm@selfmiracle.com")) {
echo "Thanks for your comments.";

}else{
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}

} else {
        echo "<h1>Error inserting data: " . mysqli_stmt_error($stmt) . "</h1>";
    }

    // Close statement
    mysqli_stmt_close($stmt);
} else {
    echo "<h1>Failed to prepare statement: " . mysqli_error($conn) . "</h1>";
}


// Close connection
mysqli_close($conn);


?>
