0

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..

1
  • for or foreach loop on to_user_id
    – u_mulder
    Commented Jul 6, 2013 at 17:16

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.