Php CodeIgniter-从多个表中删除数据

Php CodeIgniter-从多个表中删除数据,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想从3个表中删除数据,我有表user,pemohon,peserta,其中所有表都用外键相互连接 这个查询很有效,我已经在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 所以我转换成这样的CodeIgnit

我想从3个表中删除数据,我有表
user
pemohon
peserta
,其中所有表都用外键相互连接

这个查询很有效,我已经在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
所以我转换成这样的CodeIgniter

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

但是这个代码不起作用,你能修改我的代码吗?谢谢

在删除功能中,您必须提供表格数组:

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'));
}
如果不起作用,请使用查询函数($id转义)执行:

在不转义$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";
}

你可以试试这个过程

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

您可以将表名保存在数组中,并在delete()中将该数组作为参数传递。正如您可以阅读完整的示例:

很好。您也可以使用
$this->db->query(“将您在mysql中运行的查询放在这里”)
@Nidhi我已经尝试过了,$this->db->query(“从用户中删除用户,pemohon,peserta,pemohon,peserta WHERE user.id_user=pemohon.id_pemohon和pemohon.id_pemohon=peserta.id_peserta和pemohon.id_pemohon=$id”);但是不起作用,但是您在上面告诉了
这个查询起作用
,那么为什么它不起作用呢。尝试使用give static id
pemohon.id_pemohon=1
并检查
$id
是否有值?如果我使用static id是工作,但当我更改为paramater
$id
时,id不会给出值,但当我打印
$id
时,它会给出值,我想我不能直接在
$this->db->query
中插入
$id
,还有其他方法吗?您是否尝试在
删除数据功能中打印
$id
??试试这个:$this->db->query(“从user、pemohon、peserta-WHERE-user.id_user=pemohon.id_-pemohon和pemohon.id_-pemohon=peserta.id_-peserta和pemohon.id_-pemohon=”)`正如我看到的codeigniter文档,这个查询不能用查询生成器执行,你可以使用
$this->db->query()
函数。很抱歉,我还是codeigniter
和pemohon.id\u pemohon=?
函数中的第二个参数填充问号,但它因阻止sql注入而逃逸。如果我像这样插入参数
$id
pemohon.id\u pemohon=$id',它不会给出值,如果我像这样插入参数
$id` pemohon.id\u pemohon=array($id)',它给出值数组(7),但采用数组格式。任何其他方式?
pemohon.id\u pemohon=$id'
不能用值替换$id,必须使用双引号字符串的开头和结尾。最后:
pemohon.id\u pemohon=$id“
如果我使用此代码,它只是从表
pemohon
中删除了数据,而没有在其他表中删除
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));
}