使用从数据库

时间:2015-04-28 05:30:32

标签: php mysql email

我在下面的代码中遇到了发送群组邮件的一些问题。错误是 致命错误:

  

无法重新声明clean_string()(之前在声明中声明)   /home/content/83/11173683/html/AA/sendcanteen.php:74)

<?php
$d = $_REQUEST['date']; //requesting date from canteendetails.php

include("dboperation.php");
$obj    = new dboperation();
$n      = array();
$e      = array();
$str    = "select * from employee2";
$result = $obj->selectquery($str);
while ($r = mysql_fetch_assoc($result)) {
    $n[] = $r['emp_name'];
    $e[] = $r['email'];
}
for ($i = 0; $i < sizeof($n); $i++) {

    $str1     = "select * from modifycanteen where name='$n[$i]' and date='$d'";
    $result1  = $obj->selectquery($str1);
    $r        = mysql_fetch_assoc($result1);
    $email_to = $r['email'];
    $coffee   = $r['coffee'];
    $tea      = $r['tea'];
    $remarks  = $r['remarks'];

    $email_subject = "CANTEEN DETAILS";

    $email_from = "Catering Services";

    $body    = '
    <html>
    <head>
    <style>

    body, P.msoNormal, LI.msoNormal
    {
    background-position: top;
    background-color: #336699;
    margin-left:  10em;
    margin-top: 1em;
    font-family: "verdana";
    font-size:   10pt;
    font-weight:bold ;
    color:    "000000";
    }

    </style>
    </head>
    </body> 
    ';
    $message = "Mr/Mrs " . $n[$i] . "\nYour Canteen details";
    $c       = "No: of Coffee :" . $coffee . "\n ";
    $t       = "No: of Tea :" . $tea . "\n ";
    $rem     = "Remarks :" . $remarks . "\n ";

    $bodys .= "$message<br>";
    $bodys .= "$c<br>";
    $bodyss .= "$t<br>";
    $bodysss .= "$rem";

    $body = $body . $bodys . $bodyss . $bodysss;

    function clean_string($string)
    {
        $bad = array(
            "content-type",
            "bcc:",
            "to:",
            "cc:",
            "href"
        );
        return str_replace($bad, "", $string);
    }
    $email_message .= "Message: " . clean_string($message) . "\n";    
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Accounttoall.com <http://www.redboat.in/>' . "\r\n";

    @mail($email_to, $email_subject, $body, $headers);
}
if (@mail) {
    header("location:canteendetails.php");
} else {
    echo "MAIL IS NOT SENT";    
}
?>

1 个答案:

答案 0 :(得分:0)

一个函数只能定义一次

为什么要在for循环中定义functino clean_string

你必须在for循环之外定义函数

试试这个

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

$d = $_REQUEST['date']; //requesting date from canteendetails.php

include("dboperation.php");
$obj    = new dboperation();
$n      = array();
$e      = array();
$str    = "select * from employee2";
$result = $obj->selectquery($str);
while ($r = mysql_fetch_assoc($result)) {
    $n[] = $r['emp_name'];
    $e[] = $r['email'];
}
for ($i = 0; $i < sizeof($n); $i++) {

    $str1     = "select * from modifycanteen where name='$n[$i]' and date='$d'";
    $result1  = $obj->selectquery($str1);
    $r        = mysql_fetch_assoc($result1);
    $email_to = $r['email'];
    $coffee   = $r['coffee'];
    $tea      = $r['tea'];
    $remarks  = $r['remarks'];

    $email_subject = "CANTEEN DETAILS";

    $email_from = "Catering Services";

    $body    = '
    <html>
    <head>
    <style>

    body, P.msoNormal, LI.msoNormal
    {
    background-position: top;
    background-color: #336699;
    margin-left:  10em;
    margin-top: 1em;
    font-family: "verdana";
    font-size:   10pt;
    font-weight:bold ;
    color:    "000000";
    }

    </style>
    </head>
    </body> 
    ';
    $message = "Mr/Mrs " . $n[$i] . "\nYour Canteen details";
    $c       = "No: of Coffee :" . $coffee . "\n ";
    $t       = "No: of Tea :" . $tea . "\n ";
    $rem     = "Remarks :" . $remarks . "\n ";

    $bodys .= "$message<br>";
    $bodys .= "$c<br>";
    $bodyss .= "$t<br>";
    $bodysss .= "$rem";

    $body = $body . $bodys . $bodyss . $bodysss;


    $email_message .= "Message: " . clean_string($message) . "\n";    
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Accounttoall.com <http://www.redboat.in/>' . "\r\n";

    @mail($email_to, $email_subject, $body, $headers);
}
if (@mail) {
    header("location:canteendetails.php");
} else {
    echo "MAIL IS NOT SENT";    
}
?>