Pretty new to PHP, Ive been trying to combine to working codes into a single function but have not had any success. After spending all day trying to tweak it to get it to work ive decided to ask for guidance and help
Ive tried restructuring the code order but no success.
<?php
/* Global Setup */
// Declare HTML Form, Post Method Variables
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Database Setup */
// Input Validation , Variables Should not be empty
if (!empty($yourname) || !empty($subject) || !empty($comments) || !empty($email) ){
//Enter DB Credentials
$host = "localhost"; /*Godday C-Pannel MySQL Server Host Name*/
$dbname = "ContactDB"; /*Database Name*/
$dbUsername = "uncontact";
$dbPassword = "pwcontact";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
/*MySQL Insert Data Statement*/
$SQL_INSERT = "INSERT INTO contact_tbl (name, email, subject, message) values(?, ?, ?, ?)"; //Insert variables into table
//Validate Insert
if ($conn->query($SQL_INSERT) === TRUE) {
//Prepare statement
$stmt = $conn->prepare($SQL_INSERT);
$stmt->bind_param("ssss", $yourname, $email, $subject, $comments);
$stmt->execute();
echo "Record inserted sucessfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$stmt->close(); //Close Statement
$conn->close(); //Close Database Connection
/* Email Setup */
/* Set e-mail recipient */
$recipientemail = "recipient@contact.com";
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your a new form request has been submitted by:
Name: $yourname
E-mail: $email
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($recipientemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<b>We apologize for the inconvenience, an error occurred.</b><br />
<?php echo $myError; ?>
<?php
exit();
}
}
} else {
echo "All fields are required";
die(mysql_error());
}
?>
Any help would be deeply appreciated.
Below is the code after applying the changes that Barmar suggested, still not being able to make this work though. (this now includes the relocation of the function scripts)
<?php
/***********************************************************************************************/
/* Global Setup */
/***********************************************************************************************/
//Function Used to verify user form input fields
function check_input($data, $problem=''){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
//Function Used to notify user of incorrect user input
function show_error($myError){
?>
<b>We apologize for the inconvenience, an error occurred.</b><br />
<?php echo $myError; ?>
<?php
exit();
}
// Declare HTML Form, Post Method Variables
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/***********************************************************************************************/
/* Database Setup */
/***********************************************************************************************/
// Input Validation , Variables Should not be empty
if (!empty($yourname) && !empty($subject) && !empty($comments) & !empty($email) ){
//Enter DB Credentials
$host = "localhost";
$dbname = "ContactDB";
$dbUsername = "uncontact";
$dbPassword = "pwcontact";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
//error grabber
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
/*MySQL Insert Data Statement*/
$SQL_INSERT = "INSERT INTO contact_tbl (name, email, subject, message) values(?, ?, ?, ?)"; //Insert variables into table
//Validate Insert
if ($stmt = $conn->prepare($SQL_INSERT)) {
/*Prepare statement: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").
The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without executing it. */
$stmt->bind_param("ssss", $yourname, $email, $subject, $comments);
/* This function binds the parameters to the SQL query and tells the database what the parameters are. The "ssss" argument lists the types of data that the parameters are. The s character tells mysql that the parameter is a string.
The argument may be one of four types:
i - integer
d - double
s - string
b - BLOB */
$stmt->execute();
/*Execute:Application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values */
if ($stmt->execute()) {
echo "Record inserted successfully";
} else {
echo "Error: " . $stmt->error;
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$stmt->close(); //Close Statement
$conn->close(); //Close Database Connection
/***********************************************************************************************/
/* Email Setup */
/***********************************************************************************************/
/* Set e-mail recipient */
$recipientemail = "recipient@contact.com";
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your a new form request has been submitted by:
Name: $yourname
E-mail: $email
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($recipientemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/***********************************************************************************************/
}
} else {
echo "All fields are required";
}
?>