duanjue2576 2013-11-04 12:35
浏览 7

PHP,foreach邮件两次

I was wondering if you guys could help me out with something that seems really simple but I somehow dont get it to work how I want it to work.

The code below is something I made to send emails to people. But every emailadres in $allEmails is receiving 2 emails and the database is filled twice aswell but the echo in the same if is only being showed once.....

if($_POST['submitButton'] == "Versturen")
{
    $idcollecte = mysql_query("SELECT yc_collectes.id
                 FROM yc_collectes
                 WHERE yc_collectes.naampubliekecollecte = '$naampubliekecollecte'");
    $fetchidcollecte = mysql_fetch_array($idcollecte);
    $id = $fetchidcollecte['id'];

    $nummer = 1;
    $voornaam1 = $_POST['voornaam1'];
    $achternaam1 = $_POST['achternaam1'];
    $email1 = $_POST['emailadres1'];

    $voornaam2 = $_POST['voornaam2'];
    $achternaam2 = $_POST['achternaam2'];
    $email2 = $_POST['emailadres2'];

    $voornaam3 = $_POST['voornaam3'];
    $achternaam3 = $_POST['achternaam3'];
    $email3 = $_POST['emailadres3'];

    $voornaam4 = $_POST['voornaam4'];
    $achternaam4 = $_POST['achternaam4'];
    $email4 = $_POST['emailadres4'];

    $voornaam5 = $_POST['voornaam5'];
    $achternaam5 = $_POST['achternaam5'];
    $email5 = $_POST['emailadres5'];

    $allvoornamen = array($voornaam1, $voornaam2, $voornaam3, $voornaam4, $voornaam5);
    $allachternamen = array($achternaam1, $achternaam2, $achternaam3, $achternaam4, $achternaam5);
    $allEmails = array($email1, $email2, $email3, $email4, $email5);

    if($allEmails != '')
    {
        $a = 0;

        echo "<pre>";
            print_r($allEmails);;
        echo "</pre>";
        foreach($allEmails as $singleEmail)
        {
            if($singleEmail != '')
            {
                echo '1';

                //Elk emailadres in database stoppen 1x... nu 2x
                mysql_query("INSERT INTO yc_uitnodigingen (emailadres, collecteid)
                                 VALUES ('$singleEmail', $id)");
                //
                $to = $singleEmail;
                if($allvoornamen[$a] == '')
                {
                    $allvoornamen[$a] = 'Meneer/Mevrouw';
                    $allachternamen[$a] = '';
                }


                $subject = 'YouCollect Uitnodiging.';
                $message = ('
                <html>
                <body>
                <h3>Beste '.$allvoornamen[$a].''.$allachternamen[$a].', '.$name.' nodigd u uit voor een Collecte! </h3>
                <table>
                <tr>
                    <td>Dit is het uitnodigingsbericht: </td>
                    <td></td>
                    <td>Om mee te doen aan de collecte dient u op de volgende link te klikken:</td>
                    <td><a href="youcollect.nl/index.php/nl/bedanktmeedoen?naampubliekecollecte='.$naampubliekecollecte.'">Doe mee aan deze collecte!</a></td> 
                </tr>
                </table>
                </body>
                </html>
                    ');
                $headers = 'Content-type: text/html; charset=iso-8859-1' . "
";
                $headers .= 'MIME-Version: 1.0' . "
";

                mail($to, $subject, $message, $headers);
                $a++;
            }
        }
    }
}

So what I want is that every $singleemail in $allEmails is receiving 1 email and the database is being inserted once instead of twice.

Does anybody see what is wrong with the code ?

Thanks in regards

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助