 <?php
        include_once('opendb.php');
    $mess="OK";
    if (isset($_POST["Set_Name"]))
       {
            $Set_Name=$_POST["Set_Name"];
            $CompID=$_POST["comp_id"]; 
            if (isset($_POST["Set_Value"]))
              {
                   $Set_Value=$_POST["Set_Value"];
            
                    $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 ) {
                           $sql = "INSERT INTO appsettings (set_category,set_name,val_type,comp_id) VALUES ('GENERAL','".$Set_Name."','S',".$CompID.")";
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess="OK";
                           } else
                           {
                              die( "Error: Inerting settings " . $sql . "<br>" . $conn->error);
                           } 
                    }
                    if ($mess=="OK")
                    {
                        $sql = "UPDATE appsettings SET set_value='".$Set_Value."' WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
        
                        if ($conn->query($sql) === TRUE) {
                            $mess="OK";
                            if ($Set_Name=="COMP_NAME"){
                                 $sql = "UPDATE company SET comp_name='".$Set_Value."' WHERE comp_id=" .$CompID;
                                if ($conn->query($sql) === TRUE) {

                                }
                            }
                        } else {
                            die( "Error updating settings: " . $conn->error);
                        }
                    }
             }
              Else
             {
                die( "ERROR: No data settings value!");
              }
            
    }
    
    Else
    {
        die( "ERROR: No data settings name!");
    }     
    echo "OK";
    $conn->close();
?>