0

I have a code

$hasil = "INSERT INTO pendaftaran VALUES ('$no_ai','$tgl_daftar','$kode_dokter','$kode_pasien','$petugas_jaga','$bayar','$status_ambil','$catatan')";
$sql = mysql_query($hasil);

foreach($_POST['cek'] as $selected){

    foreach($_POST['cek'] as $selek){
    $res = mysql_query("SELECT kode_cek FROM master_cek where kode_item='$selected'");
    while($koko=mysql_fetch_array($res)){

    print_r($koko);


    $result = "INSERT INTO detail_daftar  VALUES ('',$no_ai','$res','0','','','$selected')";
    $sql = mysql_query($result);    
    print_r($result);

        }
    }
}

The Result is..

Resource id #5 INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')Resource id #5INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')Resource id #5INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')

I am very confused with the resource id. Please help me thank you :)

Rizier123
  • 58,877
  • 16
  • 101
  • 156

3 Answers3

0

It because when query is executed using PHP MySql driver it gives you resource id from where you need to fetch actual result.

If you need to get last insert id then you will have to make one more function call

print_r(mysql_insert_id());
shahmanthan9
  • 473
  • 1
  • 3
  • 14
0
foreach($_POST['cek'] as $selected){

foreach($_POST['cek'] as $selek){
$res = mysql_query("SELECT kode_cek FROM master_cek where kode_item='$selected'");
while($koko=mysql_fetch_array($res)){

print_r($koko);


$result = "INSERT INTO detail_daftar  VALUES ('',$no_ai','$koko['kode_cek']','0','','','$selected')";
$sql = mysql_query($result);    
print_r($result);

    }
}
}

You are trying to insert $res change that to $koko['kode_cek']

AVM
  • 592
  • 2
  • 11
  • 25
0

Insert query doesn't return array. To get inserted id of executed query you have to call last inserted id function which will return you Id of last inserted record in DB. Below function will help to get ID of inserted record.

`mysql_insert_id();`
Mayur
  • 96
  • 1
  • 9