【问题标题】:collecting data from mysql and post email从 mysql 收集数据并发布电子邮件
【发布时间】:2011-11-04 10:21:34
【问题描述】:

我将尝试解释我的问题。我有一个表格,在那个表格里面我有一个“选择一个状态”选项:

<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
   etc.....
</select>

例如:客户填写表格并选择阿拉巴马州作为州。然后,他们提交表单,表单连接到数据库,在该数据库中可以看到阿拉巴马州 (AL)。然后它从与 ALbama 部分相关的 ip 部分收集 ip,并将其与其他表单信息(姓名、电子邮件等)一起提交到我的电子邮件地址。它还需要从阿拉巴马州随机选择一个 ip,因为在数据库中我多次拥有阿拉巴马州(AL),所以它只选择阿拉巴马州 ip 中的任何一个

+-------+---------------+
| state |      ip       |
+-------+---------------+
| AL    | 67.100.244.74 |
| AK    | 68.20.131.135 |
| AZ    | 64.134.225.33 |
+-------+---------------+

感谢这个论坛上的一些人,我已经设法整理了一些 php 代码给你看。我已将此代码添加到它自己的 php 文件中,与 html 表单分开

<?php
// visit http://php.net/pdo for more details
// start error handling

try 
{
  // connect
  $pdo = new PDO('mysql:host=localhost;dbname=name', 'dbuser', 'pass');
  // enable error handling through exceptions
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // create safe query
  $query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY rand() LIMIT 1");
  // pass data & execute query (since the data are of string type
  // and therefore can be passed in this lazy way)
  $query->execute(array($_POST['State']));
  // get value
  $ip = $query->fetchColumn();
  // print out the IP address using $ip
}
catch (Exception $e)
{
  echo "sorry, there was an error.";
  mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
}
?>

接下来我将该 php 文件名添加到表单的 post 部分,如下面的代码

<form name="contactform" method="post" action="form.php">

好的,当我去测试它时,一切正常,我没有收到任何错误,但我没有收到电子邮件。我检查了垃圾邮件,但它不存在。

因此,由于我没有收到电子邮件,我想也许我需要某种 php 电子邮件表单。所以我将上面已有的代码添加到下面的代码中

<?php

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "1stoptutorials@gmail.com";
    $email_subject = "This is a test";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['State']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $state = $_POST['State']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (!mail($email_to, $email_subject, $email_message, $headers))
{
    echo "failed to send message";
}  

?>

现在,当我发送电子邮件时,我实际上收到了电子邮件,但状态部分不正确。这是我收到的电子邮件

First Name: afdf
Last Name: sfgsdf
Email: sd@fd.com
State: AZ
Comments: ali

您可以看到它处于两个字母状态,但我希望它从我的 AZ 数据库中的 ip 表中提取 ip 号。这也是我想要的样子

 First Name: afdf
    Last Name: sfgsdf
    Email: sd@fd.com
    State: 64.134.225.33
    Comments: ali

如果有人能看到我遗漏了什么以及为什么这不是从 ip 表中获取随机 IP 地址,将不胜感激

谢谢大家

阿里

【问题讨论】:

  • 显然你错过了获取状态的 ip 地址。

标签: mysql database forms phpmyadmin


【解决方案1】:
$state = $_POST['State']; // not required

在上述声明之后,您正在尝试

$email_message .= "State: ".clean_string($state)."\n";

这将发送 $_POST['State'] 的任何值。 您必须从表单中发送 ip 或从上述代码中获取 ip 并使用

$email_message .= "State: ".clean_string($ip)."\n";

【讨论】:

  • 哇,你太好了,我已经用了好几个星期了,现在我可以在电子邮件中看到 ip。它唯一没有做的就是随机选择一个ip。因此,例如在阿拉斯加州 (AL) 下,它每次都获取相同的 ip。对于每个州,我有大约 150 个不同的 ip,我希望它随机获取一个 ip。你知道我错过了什么吗?非常感谢您的所有帮助
  • 我不确定,你的代码应该给你随机值。 $query = $pdo-&gt;prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY RAND() LIMIT 1");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2013-09-27
  • 2012-05-28
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
相关资源
最近更新 更多