1

我有一个需要向许多朋友发送消息的网络应用程序。但是我遇到了一个错误..请帮助我....

下面是我的控制器

public function message(){

$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id[]','Recipients', 'required');
if($this->form_validation->run()){
     $data = $this->input->post("friend_id[]");
    unset($data['submit']);
    $this->load->model('Queries');
    $message=$this->Queries->saveMessage();
    if($message){
    echo "Success";
    }else{
    echo "error";
    }

     }
     else{ 
     echo validation_errors();
        }
     }

这是我的模特

 public function saveMessage($data){
     $i=0;
     foreach($data['friend_id'] as $friend_id){

          $record = array(
          'friend_id' => $friend_id,
          'message' => $data['message']
          );
          $this->db->insert('tbl_messages', $record);
          $i++;
             }
              return $data;
          }

当我运行它时..它显示错误并且数据库中没有插入数据

4

1 回答 1

1

希望对你有帮助 :

你的控制器方法message()应该是这样的:

public function message()
{
    $this->form_validation->set_rules('message','Message', 'required');
    $this->form_validation->set_rules('friend_id[]','Recipients', 'required');
    if($this->form_validation->run())
    {
        $friend_ids = $this->input->post("friend_id[]");
        $message = $this->input->post("message");
        unset($data['submit']);
        $this->load->model('Queries');
        $insert = $this->Queries->saveMessage($friend_ids, $message);
        if($insert)
        {
            echo "Success";
        }
        else
        {
            echo "error";
        }
     }
     else
    { 
     echo validation_errors();
    }
}

你的模型方法saveMassage()应该是这样的:

public function saveMessage($friend_ids , $message)
{
    foreach($friend_ids as $friend_id)
    {
        $record = array('friend_id' => $friend_id,'message' => $message);
        $this->db->insert('tbl_messages', $record);
    }
    return true;
}
于 2018-07-24T09:42:11.837 回答