使用数组内爆来构建插入查询mysqli

时间:2016-03-23 18:21:19

标签: php mysqli

这是我尝试使用implode构建查询 为什么不在数据库中执行

if(isset($_POST['tambah'])){
    $data1 = array(
        'id'        => $_POST['id'],
        'nama'      => $_POST['nama'],
        'jk'        => $_POST['jk'],
        'tempat'    => $_POST['tempat'],
        'tanggal'   => $_POST['tanggal'],
        'formattgl' => date('d mmmm, yyyy',strtotime('$tanggal')),
        'pekerjaan' => $_POST['pekerjaan'],
        'alamat'    => $_POST['alamat'],
        'fotonama'  => str_replace(' ', '-', $id.'.jpg'),
        'filefoto'  => move_uploaded_file($FILES['foto']['temp_name'], '..asset/img/anggota/'.$fotonama),
    );
    $db->tambahAnggota($data1);
}

现在我必须像这样构建查询

function tambahAnggota($data1){

    $kunci = implode(", ",array_keys($data1));
    $i     = 0;
    foreach ($data1 as $key => $value) {
        if (!is_int($value)){
            $arrayValue[$i] = "'".$value."'"; 
        }else{
            $arrayValue[$i] = $value;
        }
        $i++;
    }

    $nilai = implode(", ", $arrayValue);
    $s = "insert into anggota ($kunci)";
    $s .= " VALUES ";
    $s .= "($nilai)";
    $sql = $this->db->query($s); /*or die (($this->db->connect_error($s)))*/

    if($sql){
        header("location: ?op=anggota");    
    }else {
        echo "Connection failed ".$this->db->connect_errno;
    }
}

我怎样才能在上面提到的内爆函数

中做到这一点

0 个答案:

没有答案