获取两个数据库表结果并发送一封php电子邮件

时间:2015-07-18 02:18:38

标签: php mysql sql email foreach

我有两个数据库表用户& user_players我试图让它与之合作。

users
CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `phone_number` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
 `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `salt` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
 `joined` datetime NOT NULL,
 `group` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=58 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

这不是所有的user_players,而是讨论所需的全部内容。

user_players
CREATE TABLE `user_players` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,

我想向具有2,3,4和5组的用户发送php电子邮件。然后,我希望在user_players表中包含用户的名称。 (我在页面中执行随机播放并将随机播放的结果插入到user_players中以获取随机顺序)。 然后,我想在我的电子邮件中的user_players表中列出用户订单。 即:

  1. 鲍勃

  2. 苏西

  3. 吉姆

  4. $con = mysqli_connect("localhost", "root", "", "db");
    $stmt = $con->prepare("SELECT * FROM users, user_players WHERE users.`group`=2, 3, 4, 5");
    
        $order_row = mysqli_fetch_assoc($stmt);
    
        $order_id = $_POST['id'];
        $order_firstname = $order_row['firstname'];
        $order_lastname = $order_row['lastname'];
        $order_username = $order_row['username'];
        $order_email = $order_row['email'];
        $order_group = $order_row['group'];
    
        $to = $order_email;
        $subject = ' Draft Order';
        $message .= '
        <html>
        <head>
            <title>Draft Order</title>
        </head>
        <body>
            <p>Hi '.$order_firstname.',</p><br>
            <p>The Sunday Funday draft order has been created. This is a completely randomized selection.</p>
            <p>The draft order is of the following:</p><br>';
            foreach($order_row as $order) {
                echo $order_firstname;
            }
            $message .='<p>Thank you,</p>
            <p>Administration</p>
        </body>
        </html>
        ';
    
        $from = "user-requests@example.com";
        $Bcc = "user-requests-confirm@example.com";
    
        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
        // Additional headers
        $headers .= 'To: ' .$to. "\r\n";
        $headers .= 'From: ' .$from. "\r\n";
        $headers .= 'Bcc: '.$Bcc. "\r\n";
    
        // Send the email
        mail($to,$subject,$message,$headers);
    }
    

    我收到了电子邮件的关注错误..

    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home4/pfarley1/public_html/example.com/userCreator.php on line 248
    
    Notice: Undefined variable: message in /home4/pfarley1/public_html/example.com/userCreator.php on line 267
    
    Warning: Invalid argument supplied for foreach() 
    

    有谁知道如何才能使这项工作?

0 个答案:

没有答案