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

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

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

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

所需场景:

  1. | User ID | User Name | User Email |
  2. ----------------------------------------
  3. | 1 | Test 1 | test1@test.com
  4. test1@admin.com
  5. admin1@test.com
  6. ----------------------------------------
  7. | 2 | Test 2 | test2@test.com
  8. test2@admin.com
  9. admin2@test.com
  10. ----------------------------------------
  11. | 3 | Test 3 | test3@test.com
  12. test3@admin.com
  13. admin3@test.com
  14. ----------------------------------------
ht4b089n

ht4b089n1#

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

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [user_id] => 1
  6. [user_name] => Test 1
  7. [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
  8. )
  9. [1] => Array
  10. (
  11. [user_id] => 2
  12. [user_name] => Test 2
  13. [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
  14. )
  15. [2] => Array
  16. (
  17. [user_id] => 1
  18. [user_name] => Test 2
  19. [user_mail] => test3@test.com;test3@admin.com;admin3@test.com
  20. )
  21. )

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

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

视图代码:

  1. <table border="1">
  2. <thead>
  3. <tr>
  4. <th>User id</th>
  5. <th>User Name</th>
  6. <th>Emails</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <?php foreach($user_det as $val){
  11. $email = explode(';',$val['user_mail']);
  12. ?>
  13. <tr>
  14. <td><?php echo $val['user_id'] ?></td>
  15. <td><?php echo $val['user_name'] ?></td>
  16. <td>
  17. <?php foreach($email as $val2){ ?>
  18. <a href="mailto:<?php echo $val2; ?>"><?php echo $val2; ?></a><br>
  19. <?php } ?>
  20. </td>
  21. </tr>
  22. <?php } ?>
  23. </tbody>
  24. </table>

输出:

展开查看全部
plupiseo

plupiseo2#

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

  1. <!-- language: lang-php -->
  2. <?php
  3. // Database configuration
  4. $dbHost = "localhost";
  5. $dbUsername = "root";
  6. $dbPassword = "";
  7. $dbName = "email_database";
  8. // Create database connection
  9. $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  10. // Check connection
  11. if ($db->connect_error) {
  12. die("Connection failed: " . $db->connect_error);
  13. }
  14. // connects to MySQL database
  15. // insert record something like that
  16. if(isset($_POST['email_id'])
  17. {
  18. $email_ids = $_POST['email_id'];
  19. }
  20. $r=implode(",",$email_ids);
  21. $insert=mysql_query("insert into email_database(email_id) value
  22. ('$r')")
  23. // send email 20 at a time
  24. $query = "SELECT email_id FROM email_database";
  25. // echo $query;die;
  26. $result = mysqli_query($db,$query);
  27. foreach($result as $email)
  28. {
  29. $email_array = implode("," ,$email);
  30. echo $to = rtrim($email_array,",");
  31. $subject = 'the subject';
  32. $message = 'hello';
  33. $headers = 'From: webmaster@example.com' . "\r\n" .
  34. 'Reply-To: webmaster@example.com' . "\r\n" .
  35. 'X-Mailer: PHP/' . phpversion();
  36. mail($to, $subject, $message, $headers);
  37. }
  38. ?>
展开查看全部

相关问题