<?php
    include_once('opendb.php');
    $mess="Error";
    $upderrors="None";
    $currow=1;
    if (isset($_POST["data"]))
    {
       
         $xml=simplexml_load_string($_POST["data"]) or die("Error: parsing data".error_get_last());
        foreach ($xml as $Record)
          {
            $sql = "SELECT * FROM batchs WHERE prod_code='$Record->prod_code' and batch='$Record->batch' and comp_id=$Record->compid";
            $result = $conn->query($sql);
            if ($result->num_rows == 0 ) {
                   $sql = "INSERT INTO batchs (comp_id,prod_code,batch) VALUES ($Record->compid,'$Record->prod_code','$Record->batch')";
                   if ($conn->query($sql) === TRUE)
                   {
                      $mess="OK";
                   } else
                   {
                      $mess= "Error: " . $sql . "<br>" . $conn->error;
                      $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                   } 
            }
            else {
              $mess="OK";  
            }
            if ($mess=="OK")
            {
              
                $sql = "UPDATE batchs SET description = '$Record->description', 
                purch_rate = $Record->purch_rate, 
                cost = $Record->cost, 
                priceu1 = $Record->priceu1, 
                priceu2 = $Record->priceu2, 
                priceu3 = $Record->priceu3, 
                mrp = $Record->mrp, 
                expiry = '$Record->expiry', 
                quantity = $Record->quantity, 
                edit_time = '$Record->edit_time', 
                price2u1 = $Record->price2u1, 
                price2u2 = $Record->price2u2, 
                price2u3 = $Record->price2u3, 
				price3u1 = $Record->price3u1, 
                price3u2 = $Record->price3u2, 
                price3u3 = $Record->price3u3, 
                mrpu2 = $Record->mrpu2, 
                mrpu3 = $Record->mrpu3 WHERE  prod_code= '$Record->prod_code' and batch= '$Record->batch' and comp_id=$Record->compid";
                
                if ($conn->query($sql) === TRUE) {
                    $mess="OK";
                } else {
                    $mess= "Error updating batchs record: " . $conn->error;
                    $upderrors.="Error updating batchs record:<br>" . $conn->error.$sql;
                }
            }
        }
        if ($upderrors=="None"){
           echo $mess;
        }
        else {
             echo $upderrors;            
        }
    }
      Else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>