How to test if Ci successfully inserted data

前端 未结 4 1431
悲&欢浪女
悲&欢浪女 2020-12-08 14:25

In Ci, I\'ve got the following function. How do I test that the query successfully inserted without error\'s?

public function postToWall() {
    $entryData =         


        
相关标签:
4条回答
  • 2020-12-08 14:59

    You can also do it using Transactions like this:

               $this->db->trans_start();
               $this->db->query("INSERT IGNORE INTO wallPosts (entryData, entryCreationDateTime, wpChurchId)
                          VALUES('$entryData', NOW(), '$myChurchId')");
               $this->db->trans_complete();
    
               if ($this->db->trans_status() === FALSE) {
                   return "Query Failed";
               } else {
                   // do whatever you want to do on query success
               }
    

    Here's more info on Transactions in CodeIgniter!

    0 讨论(0)
  • 2020-12-08 15:01

    if you are using bootstrap on codeigniter try flash message or simply redirect

    $added = $this->your_modal->function_reference($data);
    
    
    if ($added) {
        $this->session->set_flashdata('success', 'Added successfully.');
        redirect('home');
    } else {
        $this->session->set_flashdata('error', 'Something wrong.');
        redirect('home');
    

    ON CONTROLLER

    0 讨论(0)
  • 2020-12-08 15:18

    You can use $this->db->affected_rows() function of codeigniter.

    See more information here

    You can do something like this:

    return ($this->db->affected_rows() != 1) ? false : true;
    
    0 讨论(0)
  • 2020-12-08 15:24

    I prefer use

    echo $this->db->affected_rows();
    

    in models file

    0 讨论(0)
提交回复
热议问题