简体   繁体   中英

How to Insert Data with multiple while loop in php

i'm having trouble how to insert data with multiple while loop in php

so i have 5 tables

so table

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

outbox

id    number    date    kode_brg    nama_brg    harga    jumlah

servis_detil

id    code_servis    name_servis    code_brg    name_brg    qty

this is my query

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

the query works if only use 1 while loop, but it doesnt works if using 2 while loops and no error appear the page just keep loading.

need your help to fix my query

thanks,

you need to use mysqli_fetch_assoc() instead of mysqli_fetch_row() .

Use this

while($r=mysqli_fetch_assoc($query)){
$yourVariable=$r['table field name'];
$mahdi=mysqli_query($con,"SELECT * from servis_detil where kd_servis = '$yourVariable' ");
}

NOTE-your code is open to sql injection attack use prepared statement to avoid it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM