如何在不使客户端进程挂起的情况下在 php 后台插入大数据

问题描述 投票:0回答:0

我正在尝试通过库存软件准确制作并在完成操作后更新交叉库存 但它需要 20 秒才能完成更新,因此需要在不影响客户端进程的情况下完成此过程,我使用 php 和 javascript web worker,但客户端进程仍在等待,直到 worker.js 完成进程

  <script>
 //main.js
var worker = new Worker('worker.js')
worker.postMessage('fund.php')
worker.addEventListener('message', function(e) {
data = e.data
})
</script>       





<script>
   //worker.js
 function asyncAddCounter() {
 var xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
 if (this.readyState == 4) {
 counter++;
}
};
xhttp.open("GET", "fund.php", true);
 xhttp.send();
}

asyncAddCounter();
</script>     `       


   <?php 
   //fund.php
     $query5=mysqli_query($con,"SELECT * FROM user WHERE name='$name' and user_id='$idzz'")or die(mysqli_error($con));
         
        $row5=mysqli_fetch_array($query5);
        $branch=$row5['branch_id'];
         $company=$row5['man_id'];
$request = 1;
if(isset($_GET['request']) && ($branch>0 || $company>0)){
$request = $_GET['request'];
}
// DataTable data
if(($request == 1) && ($branch>0 || $company>0)){

$empQuery = "SELECT *, price.retail as reja, pprice.retail as nunuzi from product inner join            category on (product.cat_id=category.cat_id) inner join price on (price.proid=product.prod_id) inner   join pprice on (pprice.proid=product.prod_id)    WHERE 1 and product.branch_id='$branch' and product.man_id='$company' and price.branch_id='$branch' and price.man_id='$company'and pprice.branch_id='$branch' and pprice.man_id='$company' and product.deleted='0' ";


   $empRecords = mysqli_query($con, $empQuery);
  $data = array();
 $nn=0;
while ($row = mysqli_fetch_assoc($empRecords)) {
     $pp=$row['prod_id'];
      $pri=$row['prod_id'];
 
 
     //shop qty
 $sold=mysqli_query($con,"SELECT SUM(qty) as psum from sales inner join  sales_details on (sales_details.sales_id=sales.sales_id)  inner join price on (sales_details.prod_id=price.pid) inner join product on (product.prod_id=price.proid) where sales.status='0' and sou='0' and product.prod_id='$pri'")or die(mysqli_error($con));
$rowsold = mysqli_fetch_assoc($sold);
$total_sold1=number_format($rowsold['psum']);

$sin=mysqli_query($con,"SELECT SUM(qty) AS ssum FROM stockin INNER join product on (stockin.prod_id=product.prod_id)  WHERE product.prod_id='$pri' and desti='0'")or die(mysqli_error($con));
$rowsin = mysqli_fetch_assoc($sin);
$total_in=number_format($rowsin['ssum']);

$sou=mysqli_query($con,"SELECT SUM(qty) AS sout FROM stockout INNER join product on (stockout.pro_id=product.prod_id)  WHERE product.prod_id='$pri' and sosi=0")or die(mysqli_error($con));
$rowsou = mysqli_fetch_assoc($sou);
$total_sout=number_format($rowsou['sout']);

$tin=mysqli_query($con,"SELECT SUM(qtyy) as tinsum FROM transfer_s INNER join trantem on (transfer_s.ttrid=trantem.trid) WHERE trantem.prid='$pri' and destine='0'")or die(mysqli_error($con));
$rowtin = mysqli_fetch_assoc($tin);
$total_transin=number_format($rowtin['tinsum']);

$tou=mysqli_query($con,"SELECT SUM(qtyy) as tousum FROM transfer_s INNER join trantem on (transfer_s.ttrid=trantem.trid) WHERE trantem.prid='$pri' and source='0'")or die(mysqli_error($con));
$rowtou = mysqli_fetch_assoc($tou);
$total_transou=number_format($rowtou['tousum']);

$tex=mysqli_query($con,"SELECT SUM(pqty) as exsum FROM extend WHERE prid='$pri' and sid=0")or die(mysqli_error($con));
$rowext = mysqli_fetch_assoc($tex);
$total_extend=number_format($rowext['exsum']);
      

      $shop=$start+$total_in-$total_sold1-$total_sout+$total_transin-$total_transou+$total_extend;
       $qqq=$shop;
       $re=$row['reorder'];
       $tex=mysqli_query($con,"INSERT INTO shop_stock(prod, stockin, stockout, train, traout, sold, adju, closing) VALUES ('$pri','$total_in','$total_sout','$total_transin','$total_transou','$total_sold1','$total_extend','$shop')")or die(mysqli_error($con));
       
   } ?>    `

我希望这个过程在不影响我的客户的情况下在后台运行

javascript php web backgroundworker
© www.soinside.com 2019 - 2024. All rights reserved.