我正在编写一个非常简单的表单,必须保存到MySQL数据库中。由于某种原因,一旦表单填满并按下提交按钮,下一页将显示一条消息“无法找到此页面”。此消息显示在我的wordpress框架中。我知道访问数据库没有问题,因为如果我直接在代码中写入信息(姓名,电子邮件,联系人和地址),所有这些都会出现在数据库中。
任何帮助将非常感谢。问候。
这是HTML代码。
<!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>
这是PHP代码。
<?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();
?>