我有一个非常简单的html页面,它调用一个php脚本将数据从表单提交到数据库。一旦用户提交了他们的数据,如果他们能够收到一条消息,说他们已经成功添加,然后将它们重定向到带有表单的页面,那就太好了。我使用的代码如下: HTML文件PHP表单 - 在提交后返回原始页面
<html>
<body>
<form action="insertcustomer.php" method="post" >
<p>First Name: <input type="text" name="firstname">
Last Name: <input type="text" name="lastname">
<P>Phone: <input type="text" name="phone"> Email: <input type="text" name="email"></P>
Address1: <input type="text"name="address1"> Address2: <input type="text" name="address2"> City:<input type="text" name="city">
<select name="Gender" id="Gender">
<option value="">Select One</option>
<option value = "1">Female</option>
<option value = "2">Male</option>
</select> <input value="Add" type="submit"> </form> </body>
</html>
PHP文件:
<?php
$con=mysqli_connect("centos-db.local","customerinfo","password","customerinfo");
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `customers` (`firstname`, `lastname`, `email`,
`phone`, `address1`, `address2`, `city`, `gender`) VALUES
('$_POST[firstname]','$_POST[lastname]',
'$_POST[email]','$_POST[phone]', '$_POST[address1]' ,'$_POST[address2]'
,'$_POST[city]' , '$_POST[gender]')"; if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Customer Added"; mysqli_close($con);
?>
你可以给任何帮助将不胜感激!
干杯
在''>''后面加上''到你的php文件的底部。它会在5秒后重定向到“http:// example.com/mypage.php”。 – 2015-01-21 09:59:38
完美的作品,非常感谢你! – user3243208 2015-01-21 10:02:40