我只是想将表单数据(电子邮件,日期)添加到mysql数据库中。下面是我的代码。 它看起来不错,但由于某种原因,每次我提交表格时,它都没有任何作用。我没有收到任何错误消息。提交后我看到的唯一变化是在顶部的浏览器 url 搜索字段中。"/localhost/?email=&submit=Notify+Me"
数据库已连接,表/列名称正确匹配。 表单本身根本不处理。 你能看看它有什么问题吗?
<?php
if(isset($_POST['submit'])) {
$email = trim($_POST['email']);
if(empty($_POST['email'])) {
$error = 'Please enter your email address.';
} else {
$newEmail = $email;
$date = date('Y-m-d H:i:s');
$insert = $db->prepare("INSERT INTO email_list(email, date) VALUES(:email, :date)");
$insert->bindParam(':email', $newEmail);
$insert->bindParam(':date', $date);
$insert->execute();
$result = $insert->execute();
if($result == false) {
$error = 'There seems to be a problem. Please try again.';
} else {
$success = 'Success.';
}
}
}
?>
<div id="error"><?php echo $error; ?></div>
<div id="success"><?php echo $success; ?></div>
<form action="" method="post">
<input type="text" name="email" placeholder="Enter email address"/>
<input type="submit" name="submit" value="Notify Me"/>
</form>
你需要
有method="post",但不能有type。
这是你所拥有的:
<form action="" type="post" enctype="multipart/form-data">
这是您需要的:
<form action="" method="post" enctype="multipart/form-data">
表单使用
GET 而不是 POST 提交,因为表单上有 type="post" 而不是 method="post"。Type 不是有效的属性,因此 id 默认为 GET,然后被脚本忽略,因为您正在检查 $_POST。