【发布时间】:2012-08-08 02:03:59
【问题描述】:
我有一个 html 表单,人们可以在其中订阅邮件列表。表单包括表单验证,提交表单时,数据使用 My SQL 存储在数据库中。
这是表单所在的 index.html 页面上的代码
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
这里是 send.php 的代码
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
我想创建另一个 html (unsubscribe.html) 页面,人们可以通过输入他们的电子邮件地址来取消订阅,以便他们的电子邮件地址与数据库中已经存在的相应电子邮件匹配,并将其从 My Sql 数据库中删除。 我发现这个教程很有帮助 - http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
这是我的 unsubscribe.html 页面上的表单。
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
但是当我在退订表单中输入 method="post" 时。 subscribe / index.html 上表单中的数据不会存储在 My Sql 中,而是显示为空白。 所以我猜我可能不能有两个“发布”方法?
如果有人能引导我朝着正确的方向前进,那将不胜感激。谢谢。
【问题讨论】:
-
请不要使用
mysql_*函数编写新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDO 或MySQLi。如果你不能决定哪一个,this article 会帮助你。如果你选择 PDO,here is good tutorial。 -
我不太明白,恐怕 - 你的意思是订阅表格不起作用?还是退订码有问题?
-
向我们展示您的
delete.php代码。 -
您必须运行查询以删除
email = $_POST["email_remove"]在delete.php中的记录 -
感谢 tereško 提供 mysql adivce。我会研究教程。嗨,andrewsi,当我添加取消订阅表单并填写订阅表单时,我的 SQL 数据库中的数据不再显示,而在取消订阅中添加“method=post”和 action="delete.php" 之前形式。我的退订表格也不起作用。我希望我说得通。感谢 Rishi Kalia 的查询建议。