<?php
$sql_sub = "DELETE FROM ".$module."_files WHERE id_module = :id ";
$result_sub = $conn->prepare($sql_sub);
$result_sub->bindValue(':id', $id, PDO::PARAM_INT);
$result_sub->execute();
if (isset($files_data) && is_array($files_data)){
	foreach ($files_data AS $key => $value){
		$sql_check = "SELECT file FROM files WHERE id = :id LIMIT 1";
		$result_check = $conn->prepare($sql_check);
		$result_check->bindValue(':id', $value['id_media'], PDO::PARAM_INT);
		$result_check->execute();
		if ($result_check->rowCount()){
			$arr_check = $result_check->fetch();
			$sql_tmp = "INSERT INTO ".$module."_files SET 
									id_module = :id, 
									id_media = :id_media,
									sortorder = :sortorder ";
			if (isset($value['text']) && $value['text']) $sql_tmp .= ", text = :text ";
			else $sql_tmp .= ", text = NULL ";
			
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
			$result_tmp->bindValue(':id_media', $value['id_media'], PDO::PARAM_INT);
			$result_tmp->bindValue(':sortorder', $value['sortorder'], PDO::PARAM_INT);
			if (isset($value['text']) && $value['text']) $result_tmp->bindValue(':text', $value['text'], PDO::PARAM_STR);
			
			$result_tmp->execute();
		}
		
	}
}