I am coding a very simple form which must be saved into a MySQL Database. For some reason, once the form is filled up and the Submit button is pressed, the next page shows me a message saying "This page can't be found". This messages is shown within my wordpress frame. I know there are not problems with the access to the DB because if I write the information (name, email, contact and address) directly in the code, all of them appear in the Database.
Any help will be very appreciated. Regards.
This is the HTML code.
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<div class="maindiv">
<!--HTML Form -->
<div class="form_div">
<div class="title">
<h2>Please Fill up the Form</h2>
</div>
<form action="insert.php" method="post">
<h2>Form</h2>
<label>Name:</label>
<input class="input" name="name" type="text" value="">
<label>Email:</label>
<input class="input" name="email" type="text" value="">
<label>Contact:</label>
<input class="input" name="contact" type="text" value="">
<label>Address:</label>
<textarea cols="25" name="address" rows="5"></textarea><br>
<input class="submit" name="submit" type="submit" value="Insert">
</form>
</div>
</div>
</body>
</html>
And this is the PHP code.
<?php
// Create connection
$conn = new mysqli('localhost', 'user', 'password', 'DB');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$contact = mysql_real_escape_string($_POST['contact']);
$address = mysql_real_escape_string($_POST['address']);{
$sql = "INSERT INTO MyTable (name, email, contact, address) VALUES ('$name', '$email', '$contact', '$address')";
}
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>