[英]How to Insert Data with multiple while loop in php
我在如何在php中使用多個while循環插入數據時遇到麻煩
所以我有5張桌子
如此表
soNo Customer Date_masuk Date_keluar note status
so_detail
id soNo kode_servis name_servis qty
so_detail_temp
id kode_servis nama_servis qty
發件箱
id number date kode_brg nama_brg harga jumlah
servis_detil
id code_servis name_servis code_brg name_brg qty
這是我的查詢
<?php
$con = mysqli_connect('localhost', 'root', '', 'a.karat');
header("Location: soAdd.php");
if(isset($_POST['save'])){
$soNo = $_POST['soNo'];
$customer = $_POST['customer'];
$date_masuk = $_POST['date_masuk'];
$date_keluar = $_POST['date_keluar'];
$note = $_POST['note'];
$status = $_POST['status'];
$input = mysqli_query($con, "INSERT INTO so VALUES('$soNo', '$customer', '$date_masuk', '$date_keluar','$note', '$status')") or die(mysqli_error());
if($input){
$query=mysqli_query($con,"SELECT * from so_detail_sementara ");
while($r=mysqli_fetch_row($query)){ <=== 1st While loop
$mahdi=mysqli_query($con,"SELECT * from servis_detil where kd_servis = '$r[1]' ");
while($alra=mysqli_fetch_row($mahdi)){ <=== 2nd while loop
mysqli_query($con, "insert into so_detail(soNo, kode_servis, nama_servis, qty)
values('$soNo','$r[1]','$r[2]','$r[3]')")or die(mysqli_error());
mysqli_query($con,"insert into outbox (number, date, kode_brg, nama_brg, harga, jumlah)
values('$soNo','$date_masuk','$alra[3]','$alra[4]','0','$alra[5]')")or die(mysqli_error());
如果僅使用1個while循環,則查詢有效,但如果使用2個while循環且沒有錯誤出現,則該查詢僅在頁面繼續加載時有效。
需要您的幫助以解決我的查詢
謝謝,
您需要使用mysqli_fetch_assoc()
而不是mysqli_fetch_row()
。
用這個
while($r=mysqli_fetch_assoc($query)){
$yourVariable=$r['table field name'];
$mahdi=mysqli_query($con,"SELECT * from servis_detil where kd_servis = '$yourVariable' ");
}
注意:您的代碼對sql注入攻擊開放,請使用准備好的語句來避免它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.