同時に2つの異なるテーブルからデータを削除しようとしていますが、クエリがデータを削除していないようです。最初に、ユーザーがブログを投稿しているかどうかをチェックし、クエリが真であればユーザーとブログから削除します。ここで複数のテーブルからデータを削除する
は私のコントローラです:
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>';
}
}
}
マイモデル関数ユーザーを削除する:あなたは、deleteuser機能で
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; }
}
}
前に最初の
WHERE
を指定するには、データベース内の外部キー制約のようですね。 users_studentを削除するには、ブログエントリを削除する必要があります。あなたはエラーを投稿できますか? smoore4 @ – smoore4は、私は上記のコードを実行するとエラーが出ていないがあります。モデルでは、ブログとユーザーが存在し、正常に削除されるとtrueに戻ります。コントローラに戻ったとき、 '$ isDeleted == true'を返しますが、データベースをチェックすると、そこにデータが残っています。 –
また、delete文に次のようにフィールドを指定したいとします。$ this-> db-> delete( 'users_student.id、blog_content.user_id'); – smoore4