【问题标题】:unsubscribe html form using php and my sql使用 php 和 mysql 取消订阅 html 表单
【发布时间】: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 并使用PDOMySQLi。如果你不能决定哪一个,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 的查询建议。

标签: php mysql forms formmail


【解决方案1】:

我猜你正处于学习阶段。所以,我建议你在接收帖子的页面上检查是否调用了 POST 方法。

示例:在您的 subscribe.php 中 你应该有:

<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />

在send.php中

你必须这样做:

if(!isset($_POST['subscribe'])
{
 header('location: subscribe.html');
}

您的页面必须使用isset

如果您可以显示您的 delete.php,也许我可以编辑这篇文章并为您提供进一步的帮助,但到目前为止... 需要检查,您可以使用任意数量的表单(甚至在一页上)但是,请确保它们都有不同的 ID/名称。

你的 delete.php 脚本应该是:

<?php
require ('connection.php');  // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";} 
?>

你的 delete.php 对我来说似乎没问题。

可以将以下内容添加到第 2 行 回声“”; print_r($_POST);

并在 cmets 中发布数组?

【讨论】:

  • 嗨 KarmicDice 感谢您提供有关拥有尽可能多的表单的 POST 建议和提示。是的,我仍处于学习阶段,所以了解它真的很有帮助。这是我的 delete.php 代码
  • 再次感谢 KarmicDice 的反馈。我将 subscribe.php 中的提交按钮更改为 &lt;input type="hidden" id="action" name="action" value="submitform"/&gt; &lt;input type="submit" id="submit" name="submit" value="" class="button"/&gt; 并将其添加到 my send.php if(isset($_POST['action']) &amp;&amp; $_POST['action'] == 'submitform')'
  • 订阅表单和我的 sql 数据库现在正在工作:)!但我发现我的 form_validation.js 文件也导致了问题。所以我把它拿出来,现在我没有任何表单验证:(。我认为 form_validation 文件中的这个函数$("#contact-form").submit(function(e) { e.preventDefault(); 干扰了 send.php 中的提交表单?`
  • 如果解决方案有效,请在我的答案旁边打勾?以便其他人可以看到解决方案?
猜你喜欢
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
相关资源
最近更新 更多