简体   繁体   中英

I want to delete data on condition.if data is available in another table as a foreign key then donot delete this

i have 3 tables, 1 is for bloggers, blogger id is save in the campaign table as a foreign key and also save in the categories tables as a foreign key which is the junction table actually.

i want that when blogger id is not in the compaign table then simply delete it, but when it is in the compaign table then do no delete this and paste error..

by my code: when i delete blogger and its data is in compaign table then it just delete the data from categories table(which is junction table of blogger table)

this is my model:

public function delete($id){


      if($id != $this->db->query("SELECT blogger_id FROM tbl_campaign_detail WHERE blogger_id = $id"))
    {
         $query=$this->db->query("DELETE FROM tbl_bloggers_cat WHERE blogger_id = $id AND Cat_id >2000");

        $this->db->where('blogger_id', $id);
        $this->db->delete('tbl_bloggers');
        if($this->db->affected_rows() > 0){
        return true;
        }else{
        return false;
        }
    }
    else
    {

    }


}

this is my controller:

public function delete($id){
    $result = $this->bm->delete($id);
    if($result){
        $this->session->set_flashdata('success_msg', 'Record deleted successfully');
    }
    else{
        $this->session->set_flashdata('error_msg', 'Fail to delete record');
    }
    redirect(base_url('blogger/index'));
}

view code:

<?php $id = $blogger->blogger_id;  ?>
                        <a href="<?php echo base_url('blogger/delete/'.$id); ?>" onclick="return confirm('Do you want to delete this record?');">
                            <span class="glyphicon glyphicon-trash"></span>
                        </a>

i want that when i delete record of blogger and its id is in compaign table as a foreign key, then do not delete this record as shows error message.

but through this code: when i doing this, it delete blogger's category from category table where bloggers id is passed as foreign key.. (which is junction table of blogger and another table)

you need change in delete function just like this

 function delete($id){
    $result = $this->db->query("SELECT blogger_id FROM tbl_campaign_detail WHERE blogger_id = $id")->row();
    if(!$result){
           $this->db->query("DELETE FROM tbl_bloggers_cat  WHERE blogger_id  = $id AND Cat_id > 2000");
           if($this->db->affected_rows() > 0){
                 return true;
            }
    }
    return false;
}

Rather than doing it in PHP, you can do this directly with the database schema design. When you are defining your foreign key, just on delete restrict :

create table campaign (
  campaing_id     serial   not null,
  blogger_id      integer  not null,
  ....
  foreign key (blogger_id) references blogger (blogger_id) on delete restrict
);

Now if you try to delete a record in table blogger but there are child records in table campaign , then mysql will prevent deletion and return an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM