从多个表中删除数据

时间:2017-07-13 07:59:22

标签: mysql codeigniter delete-row

我正在尝试同时从两个不同的表中删除数据,但似乎查询没有删除数据。首先,如果查询为真,我将检查用户是否已发布任何博客并从用户和博客中删除。

这是我的控制器:

public function delete(){
    if(isset($_SESSION['userLogId'])){
        $selectedId = $this->uri->segment(3);

        // getting the current image and unlink it if image exist
        $currentImage = $this->User_account_model->currentImage('student', $selectedId);

        if($currentImage != null){
            $_SESSION['current_image'] = $currentImage->photo;
        }

        $isDeleted = $this->User_account_model->deleteUser($selectedId);

        if($isDeleted == true){

            if(isset($_SESSION['current_image']) && !empty($_SESSION['current_image'])){
                unlink($_SERVER['DOCUMENT_ROOT']."/uploadfiles/users/student-img/".$_SESSION['current_image']);
                unset($_SESSION['current_image']);
            }

            echo '<script>';
            echo 'alert("User removed successfully.");';
            echo 'window.location.href = "'.base_url('account/view-user/').'";';
            echo '</script>';

        } else {

            echo '<script>';
            echo 'alert("Error while removing. Deleting user unable to processed.");';
            echo 'window.location.href = "'.base_url('account/view-user/').'";';
            echo '</script>';

        }
    }
}

我的模型功能删除用户:

function deleteUser($selectedId){
    // first check either user have posted blog
    $this->db->select('user_id');
    $this->db->from('blog_content');
    $this->db->where('user_id', $selectedId);

    $query = $this->db->get();
    $r = $query->row();
    if(!empty($r)){

        $this->db->delete('users_student, blog_content');
        $this->db->from('users_student, blog_content');
        $this->db->where('blog_content.user_id = users_student.id');
        $this->db->where('users_student.id',$selectedId);

        if($this->db->affected_rows()){
            return true;
        } else { return false; }

    } else {

        $this->db->delete('users_student');
        $this->db->where('id',$selectedId);

        if($this->db->affected_rows()){
            return true;
        } else { return false; }

    }

}

2 个答案:

答案 0 :(得分:0)

在您的deleteUser函数中,替换此代码:

$this->db->delete('users_student, blog_content');
$this->db->from('users_student, blog_content');
$this->db->where('blog_content.user_id = users_student.id');
$this->db->where('users_student.id',$selectedId);

用这个:

$this->db->delete('blog_content', array('user_id' => $selectedId));
$this->db->delete('users_student', array('id' => $selectedId));

答案 1 :(得分:0)

我已经解决了我的问题。它是要删除的Codeigniter查询格式。

注意:在Codeigniter删除查询中,请在WHERE之前先指定DELETE

function deleteUser($selectedId){
    // first check either user have posted blog
    $this->db->select('user_id');
    $this->db->from('blog_content');
    $this->db->where('user_id', $selectedId);

    $query = $this->db->get();
    $r = $query->row();
    if(!empty($r)){

        // delete from blog first
        $this->db->where('user_id',$selectedId);
        $this->db->delete('blog_content');

        if($this->db->affected_rows()){
            // then delete from user
            $this->db->where('id',$selectedId);
            $this->db->delete('users_student');

            if($this->db->affected_rows()){
                return true;
            } else { return false; }
        }

    } else {

        $this->db->where('id',$selectedId);
        $this->db->delete('users_student');

        if($this->db->affected_rows()){
            return true;
        } else { return false; }

    }

}