<?php

// Basic example of PHP script to handle with jQuery-Tabledit plug-in.
// Note that is just an example. Should take precautions such as filtering the input data.

//header('Content-Type: application/json');

include('db-connect.php');

$input = filter_input_array(INPUT_POST);

if ($input['action'] === 'edit') 
{	
    $sqlSelect="SELECT * FROM auditdpi WHERE idauditdpi='" . $input['id'] . "'";
    $records=mysqli_query($con,$sqlSelect);
	if (isset($input['issuebycimac'])) {
    $issuebycimac=$input['issuebycimac']?'yes': 'no'; } else { $issuebycimac='no'; }
	if (isset($input['sampleinstore'])) {
	$sampleinstore=$input['sampleinstore']?'yes': 'no'; } else { $sampleinstore='no'; }
    if(mysqli_num_rows($records)>0){
       
       
        $sql = "UPDATE auditdpi SET certificatenumber='" . $input['certificatenumber'] . "'" ."  , issuebycimac='" . $issuebycimac . "'" ." , moduleselection='" . $input['moduleselection'] . "'" ." , sampleinstore='" . $sampleinstore . "'" ."  WHERE idauditdpi='" . $input['id'] . "'";
        mysqli_query($con,$sql);
    }
    else {
        $sql = "INSERT INTO auditdpi SET idtrfdetails='" . $input['idtrfdetails'] . "'" ."  , certificatenumber='" . $input['certificatenumber'] . "'" ."  , moduleselection='" . $input['moduleselection'] . "'" ." , sampleinstore='" . $sampleinstore . "'" .", issuebycimac='" . $issuebycimac . "'";
    }
    mysqli_query($con,$sql);

	
} 
if ($input['action'] === 'delete') 
{
    mysqli_query($con,"DELETE FROM auditdpi   WHERE idauditdpi='" . $input['id'] . "'");
} 


//mysqli_close($mysqli);

echo json_encode($input);
?>
