0

Case : use mysqli_insert_id to INSERT into table orders_detail and DELETE on table orders_temp Error : Can't INSERT into table orders_detail and DELETE on table orders_detail

Structure table :

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

here my code :

$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());
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bernando
  • 9
  • 2
  • try adding `printf("Errormessage: %s\n", mysqli_error($con));` after your mysqli_query lines – Duane Lortie Feb 13 '17 at 02:48
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Feb 13 '17 at 02:58
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you cannot miss or ignore. – RiggsFolly Feb 13 '17 at 03:00
  • Or show us ALL the error messages from `die(mysqli_error())` – RiggsFolly Feb 13 '17 at 03:03
  • Who upvotes bad questions like this. _Please stop_ – RiggsFolly Feb 13 '17 at 03:25
  • thanks riggfolly, it's worked :D – Bernando Feb 21 '17 at 02:41

0 Answers0