如何在php中插入具有多个while循环的数据

如何在php中插入具有多个while循环的数据,php,mysql,Php,Mysql,我在php中如何使用多个while循环插入数据时遇到问题 所以我有5张桌子 那么桌子 soNo Customer Date_masuk Date_keluar note status 细节 id soNo kode_servis name_servis qty 所以,请详细说明 id kode_servis nama_servis qty 发件箱 id number date kode_brg

我在php中如何使用多个while循环插入数据时遇到问题

所以我有5张桌子

那么桌子

soNo    Customer    Date_masuk    Date_keluar    note    status 
细节

id    soNo    kode_servis    name_servis    qty
所以,请详细说明

id    kode_servis    nama_servis    qty
发件箱

id    number    date    kode_brg    nama_brg    harga    jumlah
服务费

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());

您需要使用
mysqli\u fetch\u assoc()
而不是
mysqli\u fetch\u 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注入攻击是开放的,请使用prepared语句 为了避免它


感谢Rahul的帮助,但是没有插入表servis_detil中的数据。对于第二个while循环,我应该使用query=while($alra=mysqli_fetch_assoc($mahdi)){类型吗?是的,您可以尝试上述解决方案,并尝试使用左连接,因为您使用的while循环太多,这会减慢应用程序的速度