【问题标题】:PHP/html email form no longer submitting to mysql databasePHP/html 电子邮件表单不再提交到 mysql 数据库
【发布时间】:2011-12-27 18:07:41
【问题描述】:

我创建了这个表单:

<form method="post" action="process-form.php" id="emailForm" name="emailForm"      target="_self">  
    <h4>Sign up to be notified when we go live!</h4>
<label for="email">E-mail</label>
    <input type="text" name="email" id="email" />
<input type="submit" name="submit" id="submit" value="Submit"  onclick="return alert('Thanks! Your email has been added.');">

    <p>emails will not be shared with third parties</p>
</form>

有了这个php代码

<?php

//open database connect
$username = "username";
$password = "password";
$hostname = "server"; 

$conn = mysql_connect($hostname, $username, $password);
if (!$conn) die('Could not connect: ' . mysql_error());

//emaillist is the name of the database
mysql_select_db('emaillist');

//Clean data
function clean_data($string){
  if (get_magic_quotes_gpc()){
    $string = stripslashes($string);
  }
  $string = strip_tags($string);
  return mysql_real_escape_string($string);
}

//Mail Header removal
function remove_headers($string){
  $headers = array(
        "/to\:/i",
        "/from\:/i",
        "/bcc\:/i",
        "/cc\:/i",
        "/Content\-Transfer\-Encoding\:/i",
        "/Content\-Type\:/i",
        "/Mime\-Version\:/i",
    );

    $string = preg_replace($headers, '', $string);
    return strip_tags($string);
}

//Pick up cleaned data
$email = clean_data($_POST['email']);


//Insert data
//emails is the name of the table
$query ="INSERT INTO emails (email)
  VALUES ('$email')";

mysql_query($query);

//close Connection
mysql_close($conn); 

//redirect to page
header('Location: defaultUpCyc.html');
?>

在我的测试环境中,一切都运行良好,并且在我投入使用的几天里。但是,我去我的数据库查看是否有任何电子邮件被提交并且它完全是空的。我已将测试电子邮件放入表单并检查了数据库,但什么也没有。自从我上线以来,我没有改变任何东西,所以我很茫然。这里是测试站点:http://upcycledonline.com/test/Site/defaultUpCyc.html

感谢您的帮助!

【问题讨论】:

  • 您的代码根本没有进行任何类型的错误报告,甚至没有检查事件是否成功。例如,mysql_connect 将返回一个数据库句柄,或者如果它无法连接到数据库,则返回 false。您没有检查 mysql_connect 是否返回了有效的数据库句柄,那么您的脚本应该如何知道它是否无法连接到数据库?代码中有许多点您没有检查操作是否成功,您的脚本可能在其中任何一点上失败。
  • 我应该提到我对大部分内容都是新手。
  • 附带说明,您应该开始使用 OO MySQLi 类,就像 allows you to access the functionality provided by MySQL 4.1 and above

标签: php mysql forms email


【解决方案1】:

您应该通过添加以下内容来检查您的连接是否有效:

if (!$conn) { die('Could not connect: ' . mysql_error()); }

还要检查 firebug 以确保表单与数据一起发布。您还可以回显您的查询以检查格式。否则一切看起来都很好,所以我的假设是数据库连接不好。

只是猜测:)

【讨论】:

  • 谢谢罗伯特。我添加了 if (!$conn) die('Could not connect: ' . mysql_error());在我建立连接之后。我仍然没有收到错误。我将如何回显我的查询以检查格式?
  • 您可以只使用echo $query; 或静默error_log($query);,这将出现在您服务器的错误日志中。
  • 不确定您是否可以访问 bash,但如果您可以访问 mysql -h HOSTNAME -uUSERNAME -p DATABASE,您应该可以从命令行访问您的 mysql 数据库。然后,您可以运行查询以查看它是否插入或出错。只是添加了更多调试技巧:)
  • hhmm 我不知道我是否有 bash,但我会调查一下。谢谢!
【解决方案2】:

您的测试站点上的表单标签没有定义其操作,这意味着它正在将数据发送给自己,并且 HTML 不能与数据库一起使用。

【讨论】:

  • 你会进一步扩展吗?
  • 当然,您没有将数据提交到您的 process-form.php 文件 - 请参阅测试站点上的代码:
  • 感谢 Shomz!我什至没有看到那个在里面!
  • 没问题!造成我们大部分问题的通常是小而愚蠢的事情。 :) 现在一切都好吗?
【解决方案3】:

我认为您的问题在于 onclick javascript,尝试将其删除,它可能会起作用

【讨论】:

    【解决方案4】:

    我不知道如何在这里添加代码,所以我做了一个粘贴: http://pastebin.com/baPUPhbt 你可以用这个

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2013-09-16
      • 1970-01-01
      • 2017-04-23
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      相关资源
      最近更新 更多