[英]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.