我在如何使用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循环并且没有出现错误,则该页面只会继续加载。
需要您的帮助来修复我的查询
感谢,
答案 0 :(得分:0)
您需要使用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注入攻击使用预处理语句开放的 避免它。