使用MYSQL数据发送电子邮件

时间:2013-07-10 00:45:14

标签: php mysql email

这是一个创建停车预订的学校项目。数据很好地插入数据库并发送电子邮件。我只是很难在电子邮件中显示预订信息,或者最好是使用变量显示信息?目标是让用户在收到的电子邮件中看到插入数据库的信息。

<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Reservation"; // Table name
$confirm_code=substr(number_format(time() * rand(),0,'',''),0,10);

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gname=$_POST['garage'];
$license=$_POST['license'];
$myusername=$_POST['email'];
$floor=$_POST['floor'];
$spot=$_POST['spot'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(Confirmation, Fname, Lname, Gname, License, Floor, Spot )
VALUES('$confirm_code', '$fname', '$lname', '$gname', '$license', '$floor', '$spot')";
$result=mysql_query($sql);


// if suceesfully inserted data into database, send confirmation link to email 

      if($result){
// ---------------- SEND MAIL FORM ----------------

      // send e-mail to ...

      $to=$myusername;

    // Your subject

      $subject="Your Parking Reservation is Confirmed";

    // From

      $header="from: Luxury Parking <luxuryparking.comeze.com>";

    // Your message

      $message=" \r\n";

      $message.="The following is your reservation information. \r\n";

      $message.=$result;

      // send email

      $sentmail = mail($to,$subject,$message,$header);
    }

      // if not found 

      else {

      echo "Not found your email in our database";

      }

      // if your email succesfully sent

      if($sentmail){

    header("Location: reservationmade.php");

      }

      else {

      echo "Cannot send Confirmation link to your e-mail address";

      }
    ?>

1 个答案:

答案 0 :(得分:2)

查询完成后,MySql在$ result中返回一个布尔值。

您只需使用认知声明手动将所有值添加到电子邮件中。

示例:

$message .= $fname;
$message .= $lname;
$message .= $gname;
$message .= $license;
$message .= $myusername;
$message .= $floor;
$message .= $spot;

而不是

$message .= $result;

很可能只是将1或0放入电子邮件中。