2013-07-05 50 views
0

我有一個允許人們互相發送消息的表單,用戶可以選擇多個人發送消息,當我提交一個表單時,它會給出我選擇的人的姓名和ID消息在數組中。 uptil這裏我能夠得到它的單個收件人在基於數組的mysql表中添加多行

我希望能夠利用這個陣列,並在MySQL表的不同行每個用戶接入信息工作

這是我得到的數組當我提交表單

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

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

這是代碼,對於單個收件人工作的一部分,但不是多個

$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>"; 

將非常感謝所有幫助..

+0

'for'或''上to_user_id' foreach'環 –

回答

0

這是對我工作,我希望這可以幫助其他人在相似的位置

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