<?php
if ((!isset($_POST['submit']) && isset($id)) OR (isset($_POST['submit']) && $_POST['submit'] != 'reload' && isset($id) && !isset($error))){
	$field = array();
	$count_fields = 0;
	$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 $db_key => $db_value){
			$db_column_meta = $result->getColumnMeta($ii);
			$field[$ii] = $db_column_meta['name'];
			if ($db_value != NULL){
				switch($db_column_meta['native_type']){
					case 'BLOB': 
						$is_json = false;
						if (is_string($db_value) && mb_check_encoding($db_value, 'UTF-8')) {
							try {
		            json_decode($db_value, true, 512, JSON_THROW_ON_ERROR);
		            $is_json = true;
			        } catch (Throwable $e) { }
						}
						if ($is_json) {
							${$db_key} = json_decode($db_value, true, 512, JSON_THROW_ON_ERROR);
							break;
						}
						if (isset($raw_blob_fields) && is_array($raw_blob_fields) && in_array($db_key, $raw_blob_fields, true)) {
							${$db_key} = stripcslashes($db_value);
						} else {
							${$db_key} = stripcslashes(str_replace("'", "&#39;", str_replace("<br />","",str_replace("<br>","\n", $db_value))));
						}
						break;
					case 'LONGLONG':
					case 'LONG':
					case 'SHORT':
					  if (($action ?? null) === 'copy' && $db_key === 'datetime') {
					    ${$db_key} = time();
					  } else {
					    ${$db_key} = $db_value;
					  }
						break;
					case 'NEWDECIMAL':
					case 'FLOAT':
					case 'DOUBLE':
						${$db_key} = str_replace(".", ",", $db_value);
						break;
					default:
						${$db_key} = stripcslashes(str_replace("'", "&#39;", $db_value));
						break;
				}
			} else {
				${$db_key} = $db_value;
			}
			$ii++;
		}
	}
}