<?php
if ((!isset($_POST['submit']) && isset($id)) OR (isset($_POST['submit']) && $_POST['submit'] != 'reload' && isset($id) && !isset($error))){
	$sql = "SELECT * FROM ".$module." WHERE id = :id LIMIT 1";
	$result = $conn->prepare($sql);
	$result->bindValue(':id', $id, PDO::PARAM_INT);
	$result->execute();
	if ($result->rowCount()){
		$arr = $result->fetch();
		$count_fields = $result->columnCount() - 1;
		$ii = 0;
		foreach ($arr AS $key => $value){
			$type = $result->getColumnMeta($ii);
			$field[$ii] = $type['name'];
			if ($value != NULL){
				switch($type['native_type']){
					case 'BLOB': 
						$is_json = false;
						if (is_string($value) && mb_check_encoding($value, 'UTF-8')) {
							try {
		            json_decode($value, true, 512, JSON_THROW_ON_ERROR);
		            $is_json = true;
			        } catch (Throwable $e) { }
						}
						if ($is_json) {
							${$key} = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
							break;
						}
						if (isset($raw_blob_fields) && is_array($raw_blob_fields) && in_array($key, $raw_blob_fields, true)) {
							${$key} = stripcslashes($value);
						} else {
							${$key} = stripcslashes(str_replace("'", "&#39;", str_replace("<br />","",str_replace("<br>","\n", $value))));
						}
						break;
					case 'LONGLONG':
					case 'LONG':
					case 'SHORT':
					  if (($action ?? null) === 'copy' && $key === 'datetime') {
					    ${$key} = time();
					  } else {
					    ${$key} = $value;
					  }
						break;
					case 'NEWDECIMAL':
					case 'FLOAT':
					case 'DOUBLE':
						${$key} = str_replace(".", ",", $value);
						break;
					default:
						${$key} = stripcslashes(str_replace("'", "&#39;", $value));
						break;
				}
			} else {
				${$key} = $value;
			}
			$ii++;
		}
	}
}