数据循环并插入数据库?

时间:2017-09-19 10:25:15

标签: php mysql json

我有一个两行数组,我需要满足两个条件,并根据该条件我应该将值插入数据库。

使用此查询获取值并且正在比较$ record ['QTY']& $ record ['FILLED'],条件应该由数据库中的每一行满足,当条件满足时应插入或不插入

在这种情况下,如何检查每一行并满足条件

我如何检查循环?

<?php

    // Include confi.php
    include_once('config.php');

    if($_SERVER['REQUEST_METHOD'] == "POST"){
        // Get data
        $employee_id = isset($_POST['EMPLOYEE_ID']) ? mysql_real_escape_string($_POST['EMPLOYEE_ID']) : "";
        $mobile = isset($_POST['DRMOBILE']) ? mysql_real_escape_string($_POST['DRMOBILE']) : "";
        $qty = isset($_POST['QTY']) ? mysql_real_escape_string($_POST['QTY']) : "";
        $createdon = isset($_POST['CREATEDON']) ? mysql_real_escape_string($_POST['CREATEDON']) : "";
        $status = isset($_POST['STATUS']) ? mysql_real_escape_string($_POST['STATUS']) : "";


            $select="SELECT IFNULL(A.QTY,0)QTY,IFNULL(SUM(B.FILLED),0)FILLED,A.PHY_ID
                       FROM PHYSAMPLE A
                       LEFT JOIN FILLEDQTYS B ON A.DRMOBILE=B.DRMOBILE AND A.EMPLOYEE_ID=B.EMPLOYEE_ID AND A.PHY_ID=B.PHY_ID
                       WHERE A.DRMOBILE='$mobile' GROUP BY B.PHY_ID";
                $query=mysql_query($select);

                while($record=mysql_fetch_assoc($query)){
                        // print_r($record);

                    if($record['QTY'] < 0.7*($record['FILLED']) )
                    {
                        $json = array("status" => 2,"msg" => "Error In adding Data");
                    }else{
                        echo $sample="INSERT INTO `PHYSAMPLE` (EMPLOYEE_ID,DRMOBILE,QTY,CREATEDON) 
                        VALUES('$employee_id','$mobile','$qty','$createdon')";
                        $qry=mysql_query($sample);                
                    }
                }


    }else{
        $json = array("status" => 0, "msg" => "Request method not accepted");
    }

    @mysql_close($conn);

    /* Output header */
    header('Content-type: application/json');
    echo json_encode($json);
    ?>

示例数组

Array
(
    [QTY] => 30
    [FILLED] => 27
    [PHY_ID] => 1
)
Array
(
    [QTY] => 20
    [FILLED] => 24
    [PHY_ID] => 2
)

0 个答案:

没有答案