使用php codeigniter在mysql的一列中插入和获取多封电子邮件?

zed5wv10  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(293)

我是新来的 MySQL DB, 我正在尝试插入多封用分号分隔的电子邮件。我想把它们插入到一个单元格中(想将一系列电子邮件存储到一个单元格中)。这样所有的数据都被插入到一条记录中。有可能吗?如果是,请帮助我。目前,我正在将值传递到文本字段中。
所以当我取它们的时候,我可以很容易地把它们放在一条新的线上。并且可以直接向特定的邮件id发送电子邮件。
当前场景:

| User ID | User Name | User Email |
----------------------------------------
| 1       | Test 1    | test1@test.com;test1@admin.com;admin1@test.com
----------------------------------------
| 2       | Test 2    | test2@test.com;test2@admin.com;admin2@test.com
----------------------------------------
| 3       | Test 3    | test3@test.com;test3@admin.com;admin3@test.com
----------------------------------------

所需场景:

| User ID | User Name | User Email |
----------------------------------------
| 1       | Test 1    | test1@test.com
                        test1@admin.com
                        admin1@test.com
----------------------------------------
| 2       | Test 2    | test2@test.com
                        test2@admin.com
                        admin2@test.com
----------------------------------------
| 3       | Test 3    | test3@test.com
                        test3@admin.com
                        admin3@test.com
----------------------------------------
ht4b089n

ht4b089n1#

codeigniter返回用户数据数组表单模型

Array
(
    [0] => Array
        (
            [user_id] => 1
            [user_name] => Test 1
            [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
        )

    [1] => Array
        (
            [user_id] => 2
            [user_name] => Test 2
            [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
        )

    [2] => Array
        (
            [user_id] => 1
            [user_name] => Test 2
            [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
        )

)

将此数组传递给您的视图。

$data['user_det'] = $user_data;
$this->load->view('your_view',$data);

视图代码:

<table border="1">
    <thead>
        <tr>
            <th>User id</th>
            <th>User Name</th>
            <th>Emails</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($user_det as $val){
        $email = explode(';',$val['user_mail']);
        ?>
        <tr>
            <td><?php echo $val['user_id'] ?></td>
            <td><?php echo $val['user_name'] ?></td>

            <td>    
            <?php  foreach($email as $val2){ ?>     
                <a href="mailto:<?php echo $val2; ?>"><?php echo $val2; ?></a><br>
            <?php } ?>
            </td>
        </tr>
        <?php } ?>
    </tbody>
</table>

输出:

plupiseo

plupiseo2#

我已经添加了简单的从数据库获取邮件和发送多个请尽快检查这个。

<!-- language: lang-php -->
        <?php 
        // Database configuration
        $dbHost     = "localhost";
        $dbUsername = "root";
        $dbPassword = "";
        $dbName     = "email_database";

        // Create database connection
        $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

        // Check connection
        if ($db->connect_error) {
            die("Connection failed: " . $db->connect_error);
        }
        // connects to MySQL database 
        // insert record something like that 
        if(isset($_POST['email_id'])
        {
         $email_ids = $_POST['email_id'];
        }
        $r=implode(",",$email_ids);
        $insert=mysql_query("insert into email_database(email_id) value 
        ('$r')")
        // send email 20 at a time 
        $query = "SELECT email_id FROM email_database"; 
        // echo $query;die;
        $result = mysqli_query($db,$query);
        foreach($result as $email)
        {
            $email_array = implode("," ,$email);
            echo $to      =  rtrim($email_array,",");
            $subject = 'the subject';
            $message = 'hello';
            $headers = 'From: webmaster@example.com' . "\r\n" .
           'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
            mail($to, $subject, $message, $headers);
          }
          ?>

相关问题