案例:使用mysqli_insert_id在表orders_temp上INSERT到表orders_detail和DELETE 错误:无法在表orders_detail
上插入表orders_detail和DELETE结构表:
orders = id_oders [int] Primary Key AUTO_INCREMENT
orders_detail = id_orders[int] (No Primary Key, No AUTO_INCREMENT)
orders_temp = id_orders_temp[int] Primary Key AUTO_INCREMENT
这里是我的代码:
$tgl_skrg = date("Ymd");
$jam_skrg = date("H:i:s");
$id = mysqli_fetch_array(mysqli_query($con, "SELECT id_kustomer FROM kustomer WHERE email='$email' AND password='$password'"));
// mendapatkan nomor kustomer
$id_kustomer=$id[id_kustomer];
// simpan data pemesanan
$query_order = mysqli_query($con, "INSERT INTO orders(tgl_order,jam_order,id_kustomer) VALUES('$tgl_skrg','$jam_skrg','$id_kustomer')") ;
// mendapatkan nomor orders
$id_orders=mysqli_insert_id($con, $query_order);
// panggil fungsi isi_keranjang dan hitung jumlah produk yang dipesan
$isikeranjang = isi_keranjang();
$jml = count($isikeranjang);
// simpan data detail pemesanan
for ($i = 0; $i < $jml; $i++){
mysqli_query($con, "INSERT INTO orders_detail(id_orders, id_produk, jumlah)
VALUES('$id_orders',{$isikeranjang[$i]['id_produk']}, {$isikeranjang[$i]['jumlah']})") or die(mysqli_error());
}
// setelah data pemesanan tersimpan, hapus data pemesanan di tabel pemesanan sementara (orders_temp)
for ($i = 0; $i < $jml; $i++) {
mysqli_query($con, "DELETE FROM orders_temp
WHERE id_orders_temp = {$isikeranjang[$i]['id_orders_temp']}") or die(mysqli_error());
}