使用PHP CodeIgniter在MYSQL中的单个列中插入和获取多个电子邮件?

时间:2018-05-10 06:22:18

标签: php mysql codeigniter codeigniter-3 crud

我是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
----------------------------------------

2 个答案:

答案 0 :(得分:0)

我已经从数据库中添加了简单的提取邮件并发送了多个请尽快查看。

       <!-- 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);
          }
          ?>

答案 1 :(得分:0)

CodeIgniter返回 user_data 数组表单模型

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>

Out Put:

enter image description here