I'm setting up a contact form for my domain I have succeeded in created the database and tables from the cPanel but I'm having trouble getting the PHP code to work. I was able to establish a connection between the HTML form and the Database using PHP. When I hit "SUBMIT" on the form, it connects to the database and I get a 'success' message.
However, when I log in and check the MySQL Database through PHPmyadmin I can see that it indeed created a new row in the table, but it is blank.
I have spent hours combing the syntax line by line and can't seem to find anything out of place and I have now added a bunch of troubleshooting steps in to try and solve it.
Any help is greatly appreciated!
The HTML form is as follows:
<form action="insert.php" method="post" name="sentMessage" id="contactForm">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" name="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" name="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" name="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" name="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" value="submit" class="btn btn-xl">Send Message</button>
</div>
</form>
And the PHP file connecting the HTML form to MySQL is:
<?PHP
$link = mysqli_connect('localhost', myUserName', 'myPassword', 'myDatabase');
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($_POST['name'], $link);
$email = mysqli_real_escape_string($_POST['email'], $link);
$phone = mysqli_real_escape_string($_POST['phone'], $link);
$message = mysqli_real_escape_string($_POST['message'], $link);
$sql = "INSERT INTO 1stChoice_Contact (name, email, phone, message) VALUES ('$name', '$email', '$phone', '$message')";
if(mysqli_query($link, $sql)){
echo "Your message has been sent!";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = 'sample@yourgmail.com';
$email_body = "You have received a new message from your website contact form.
"."Here are the details:
Name: $name
Email: $email_address
Phone: $phone
Message:
$message";
$headers = "From: youremail@yourdomain.com
";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
header('location:index.html');
?>
You seem to be missing your opening form tag.
also mysqli_real_escape_string first param is the connection, second param is the string and you are not selecting a database.
I suggest having a read up on
http://www.w3schools.com/php/func_mysqli_connect.asp http://www.w3schools.com/php/func_mysqli_real_escape_string.asp