<?php
 include_once 'opendb.php';
  $ret="Error";
    if ( isset($_GET["CoID"])  && isset($_GET["SerialName"]) )
    {
        $CompID=$_GET["CoID"];
        $Set_Name=$_GET["SerialName"];
        $sql = "SELECT * FROM appsettings WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
        $result = $conn->query($sql);
        if ($result->num_rows == 0 ) {
                 
                $mqr=500001 ;
            
           $sql = "INSERT INTO appsettings (set_category,set_name,set_value,val_type,comp_id) VALUES ('GENERAL','".$Set_Name."',$mqr,'S',".$CompID.")";
           if ($conn->query($sql) === TRUE)
           {} else {
              die( "Error: Inerting settings " . $sql . "<br>" . $conn->error);
           } 
        }
      
        $conn->autocommit(FALSE);
        $conn->query("BEGIN;");
        $sql = "SELECT * FROM appsettings WHERE set_category='GENERAL' and set_name='$Set_Name' and comp_id=$CompID for update";
        $result = $conn->query($sql);
        $ret=0; 
        if ($result->num_rows > 0 ) {
            if($row = $result->fetch_assoc()) 
            {
                if (is_null($row["set_value"]))
                {                     
                  $ret=500000;                      
                } else{
                     $ret=$row["set_value"];
                    
                }
                $newval=$ret+1;
                $sql = "update appsettings set set_value='$newval'  WHERE set_category='GENERAL' and set_name='$Set_Name' and comp_id=$CompID";
                $conn->query($sql);
            }
        }
        $conn->commit();
    }
    else
    {
        $ret= "Error: No Parameters:";
    }
    echo $ret;
    $conn->close();
?>