mysql - 从多个表中删除数据

标签 mysql codeigniter delete-row

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

这是我的 Controller :

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; }

    }

}

最佳答案

在您的 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));

关于mysql - 从多个表中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45074738/

相关文章:

php - 代码点火器 fatal error : Call to a member function query() on a non-object

php - 从数据库中删除最早 7 天的行

java - 使用 C3P0 和 Hibernate/Spring 创建的许多线程

mysql - 在多列上搜索mysql中的一系列值

php - Android 的 MySQL 连接问题

php - 来自 codeigniter 中输入插入的 NULL 变量值

MySQL错误语法

php - CodeIgniter - 表单验证和 POST 数据

PostgreSQL:删除除最近日期以外的所有日期

PHP 按钮删除 MYSQL 表输出中的一行