2

I want to delete data from 3 table,i have table user,pemohon,peserta where all table is connected each other with foreign key.

this query work,I've tried on mysql

DELETE user,pemohon,peserta
FROM user,pemohon,peserta
WHERE user.id_user=pemohon.id_pemohon AND
pemohon.id_pemohon=peserta.id_peserta AND pemohon.id_pemohon=1

so i convert to CodeIgniter like this

function delete_data($id)
{
    $this->db->where('pemohon.id_pemohon=user.id_user');
    $this->db->where('pemohon.id_pemohon=peserta.id_peserta');
    $this->db->where('pemohon.id_pemohon',$id);
    $this->db->delete('pemohon','user','peserta');
}

but this code not work, can you fix my code? Thanks

8
  • 1
    Fine. You can also use $this->db->query("Put your query here which you run in mysql") Commented May 29, 2017 at 6:38
  • @Nidhi i've tried it,$this->db->query("DELETE user,pemohon,peserta FROM user,pemohon,peserta WHERE user.id_user=pemohon.id_pemohon AND pemohon.id_pemohon=peserta.id_peserta AND pemohon.id_pemohon=$id"); but not work Commented May 29, 2017 at 6:56
  • but you told above this query work then Why it does not work?? .Try it with give static id pemohon.id_pemohon=1 and also check $id have value or not? Commented May 29, 2017 at 7:28
  • if i use static id is work, but when i change to paramater $id the id not give the value, but when i print $id it give value, i think i just can't Directly insert $id in $this->db->query is there any other way? Commented May 29, 2017 at 7:52
  • Are you tried to print $id in delete_data function ?? try this : $this->db->query("DELETE user,pemohon,peserta FROM user,pemohon,peserta WHERE user.id_user=pemohon.id_pemohon AND pemohon.id_pemohon=peserta.id_peserta AND pemohon.id_pemohon='".$id."'");` Commented May 29, 2017 at 8:13

3 Answers 3

6

In delete function you must be provide array of tables:

function delete_data($id)
{
    $this->db->where('pemohon.id_pemohon=user.id_user');
    $this->db->where('pemohon.id_pemohon=peserta.id_peserta');
    $this->db->where('pemohon.id_pemohon',$id);
    $this->db->delete(array('pemohon','user','peserta'));
}

If it not worked, execute with query function ($id escaped):

function delete_data($id)
{
    $sql = "DELETE user,pemohon,peserta 
        FROM user,pemohon,peserta 
        WHERE user.id_user=pemohon.id_pemohon 
        AND pemohon.id_pemohon=peserta.id_peserta 
        AND pemohon.id_pemohon= ?";

    $this->db->query($sql, array($id));
}

And without escaping $id:

function delete_data($id)
{
    $this->db->query("DELETE user,pemohon,peserta 
        FROM user,pemohon,peserta 
        WHERE user.id_user=pemohon.id_pemohon 
        AND pemohon.id_pemohon=peserta.id_peserta 
        AND pemohon.id_pemohon= $id";
}
Sign up to request clarification or add additional context in comments.

7 Comments

As i see codeigniter documentation, this query cannot be execute with query builder and you can use $this->db->query() function.
Sorry i'm still newbie in codeigniterAND pemohon.id_pemohon= ?
Question marks filling by second parameter in $this->db->query() function, but it escaped for preventing sql injection.
if i insert parameter $id like this pemohon.id_pemohon=$id' it not give value and if i insert parameter $id` like this `pemohon.id_pemohon=array($id)' it's give the value array(7) but in array format. any other way?
pemohon.id_pemohon=$id' cannot replace $id with value, must be use double quotes start and end of string. at the end: pemohon.id_pemohon=$id"
|
2

you can try this process

function delete_data($id)
{
   $this->db->delete('user', array('id_user' => $id)); 
   $this->db->delete('pemohon', array('id_pemohon' => $id));
   $this->db->delete('peserta', array('id_peserta' => $id));
}

1 Comment

if i use this code, it just deleted data from table pemohon but not deleted in other table
0

You can save table name within an array and pass that array as parameter in delete(). As you can read full example : https://tutorialpace.com/CodeIgniter-Tutorials/CodeIgniter-Database-Delete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.