0

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();
?>
  • Is your PHP called insert.php? Is it in the same directory as your HTML? – Jay Blanchard Jul 26 '16 at 18:14
  • 3
    You cannot mix MySQL API's in your PHP code, that will not work. – Jay Blanchard Jul 26 '16 at 18:15
  • Thank you for your answer, could you be please tell me if besides the POST variables ($name, $email, $contact, $address) there are more variables using MySQL. The thing is that I'm having a loooot of difficulties identifying which is MySQL and which one is MySQLi. Thanks. And yeah. PHP is in the same directory as the HTML. – Jorge Chacón Solar Jul 28 '16 at 07:12

0 Answers0