更新查询Codeigniter消息

时间:2019-03-22 08:12:43

标签: php codeigniter

我正在运行更新查询以更新图书馆中的书籍记录。现在的问题是,如何在更新成功后而不是更新成功后打印成功消息?
更新后,它将在空白页上重定向。

我的模特是

function update_book($data, $id)
{
  $this->load->database();
  $this->db->where("id", $id);
  $query =$this->db->update("books", $data);

  if ($this->db->affected_rows() > 0)
  {
    $response = array(
           'message' => "User edited successfully",
           'status' => true
           );
  }
  else
    $response = array(
         'message' => "There is nothing to update",
         'status' => false
        );

  return $response;
}

且控制器为:

public function edit_info()
{
  $id = $this->input->post('id');
  $book_name = $this->input->post('book_name');
  $book_author = $this->input->post('book_author');
  $book_publisher = $this->input->post('book_publisher');
  $book_pages = $this->input->post('book_pages');
  $book_price = $this->input->post('book_price');
  $book_stock = $this->input->post('book_stock');

  $book_data = array(  
            'id'=>$id,
            'book_name'=>$book_name,
            'book_author'=>$book_author,
            'book_publisher'=>$book_publisher,
            'book_pages'=>$book_pages,
            'book_price'=>$book_price,
            'book_stock'=>$book_stock
          );

  $this->load->model('Book');
  $result = $this->Book->update_book($book_data, $id);

  if(in_array(1, $result))
  {
    $this->session->set_flash-data('MSG', 'Book Record Updated!!');
  }
  else
  {
    echo "FALSE";
  }
}

2 个答案:

答案 0 :(得分:1)

if($result['status'] == true){
    echo $result['message'];
    $this->session->set_flashdata('MSG', 'Book Record Updated!!');
}
else{
    echo $result['message'];
}

在您的重定向空白页面echo $this->session->flashdata('MSG');

答案 1 :(得分:0)

尝试这样

模型

function update_book($data, $id)
{
  $this->load->database();
  $this->db->where("id", $id)
  return $this->db->update("books", $data);
}

控制器

 $this->load->model('Book');
 $result = $this->Book->update_book($book_data, $id);
 if($result){

     $response = array(
       'message' => "User edited successfully",
       'status' => true
       );
  }else{

     $response = array(
     'message' => "There is nothing to update",
     'status' => false
    );
  }