adding multiple rows in mysql table based on an array

Baig

I have a form which allows people to message each other, a user can select multiple people to message, when i submit a form it gives me the name and id of people selected to message in an array. uptil here i am able to get it to work for a single recipient

I want to be able to use this array and INSERT message for each user in different rows of mysql table

this is the array that i get when i submit a form

Array (
[to_user_id] => Array
    (
        [0] => 54
        [1] => 55
    )

[subject] => aaa
[message] => bbb
[send_message] => 

this is the part of code that works for a single recipient but not multiple

$to_user_id_array = ($_POST['to_user_id']);
$params = array(
            ':to_user_id' => $to_user_id_array,
            ':subject' => $_POST['subject'],
            ':message' => $_POST['message'],
            ':sender_id' => $this->user_id,
            ':status' => "0",
            ':type' => "message",
            ':sender_name' => $sender_name,
            ':to_user_name' => $to_user_name,
            ':delete_received' => 'no',
            ':delete_sent' => 'no',

        );


        $sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
                                VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";
        parent::query($sql, $params);
        $this->error = "<div class='alert alert-success'>" . _('Your message has been sent.') . "</div>";

Will really appreciate any help..

Baig

This is what worked for me, i hope this helps someone else in similar position

while ($value = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $params = array(
        ':to_user_id' => $value['user_id'],
        ':subject' => $_POST['subject'],
        ':message' => $_POST['message'],
        ':sender_id' => $this->user_id,
        ':status' => "0",
        ':type' => "message",
        ':sender_name' => $sender_name,
        ':to_user_name' => $value['name'],
        ':delete_received' => 'no',
        ':delete_sent' => 'no',
    );
    $sql = "INSERT INTO `messages` (`sender_id`,`subject`,`comment`,`to_user_id`,`status`,`type`,`sender_name`,`to_user_name`,`delete_received`,`delete_sent`)
               VALUES (:sender_id, :subject, :message, :to_user_id, :status, :type, :sender_name,:to_user_name,:delete_received,:delete_sent);";

    parent::query($sql, $params);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL - Merge rows in table based on multiple criteria

From Dev

Adding multiple rows in Array

From Dev

Adding multiple returned MySQL rows to the same array key element in PHP

From Dev

Adding multiple rows of a table to another table

From Dev

How insert multiple rows in mysql table with php array?

From Dev

update Multiple Rows Into A Mysql Table Using Php Array

From Dev

Adding/Deleting table rows based on Select value

From Dev

Adding rows to a table based on number in another row

From Dev

jQuery, adding multiple table rows is not working correctly

From Dev

Combine multiple rows in a MySQL table

From Dev

Adding or deleting table rows with MySQL, jQuery and Ajax

From Dev

Adding Array of Buttons to Appropriate Table Rows

From Dev

Adding Array of Buttons to Appropriate Table Rows

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

Select multiple rows from table based on ID

From Dev

Intersect multiple rows of the table based on conditions

From Dev

MySQL: Range based on rows in external table

From Dev

MySQL: Range based on rows in external table

From Dev

MySQL selecting rows based on condition of additional table

From Dev

MySQL - Query For Rows With Updates Based On Another Table

From Dev

MySQL group multiple rows based on DISTINCT value

From Dev

MYSQL Select from tables based on multiple rows

From Dev

MySQL: Clone multiple rows in Table A and related rows in Table B

From Dev

Adding missing rows from the another table based on 2 columns

From Dev

R: adding in rows of zero based on the values in multiple columns

From Dev

Adding multiple cells in a column based on a value in another column but same rows

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

combine multiple rows value in mysql table

From Dev

MYSQL - UPDATE multiple rows from another table

Related Related

  1. 1

    MySQL - Merge rows in table based on multiple criteria

  2. 2

    Adding multiple rows in Array

  3. 3

    Adding multiple returned MySQL rows to the same array key element in PHP

  4. 4

    Adding multiple rows of a table to another table

  5. 5

    How insert multiple rows in mysql table with php array?

  6. 6

    update Multiple Rows Into A Mysql Table Using Php Array

  7. 7

    Adding/Deleting table rows based on Select value

  8. 8

    Adding rows to a table based on number in another row

  9. 9

    jQuery, adding multiple table rows is not working correctly

  10. 10

    Combine multiple rows in a MySQL table

  11. 11

    Adding or deleting table rows with MySQL, jQuery and Ajax

  12. 12

    Adding Array of Buttons to Appropriate Table Rows

  13. 13

    Adding Array of Buttons to Appropriate Table Rows

  14. 14

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  15. 15

    Select multiple rows from table based on ID

  16. 16

    Intersect multiple rows of the table based on conditions

  17. 17

    MySQL: Range based on rows in external table

  18. 18

    MySQL: Range based on rows in external table

  19. 19

    MySQL selecting rows based on condition of additional table

  20. 20

    MySQL - Query For Rows With Updates Based On Another Table

  21. 21

    MySQL group multiple rows based on DISTINCT value

  22. 22

    MYSQL Select from tables based on multiple rows

  23. 23

    MySQL: Clone multiple rows in Table A and related rows in Table B

  24. 24

    Adding missing rows from the another table based on 2 columns

  25. 25

    R: adding in rows of zero based on the values in multiple columns

  26. 26

    Adding multiple cells in a column based on a value in another column but same rows

  27. 27

    Insert multiple rows into a MySQL database from a table

  28. 28

    combine multiple rows value in mysql table

  29. 29

    MYSQL - UPDATE multiple rows from another table

HotTag

Archive