diff --git a/CHANGELOG.md b/CHANGELOG.md index 711b0de6..637b9fac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Autofocus added field in alter table - Executed SQL commands: Add button for copy to clipboard - Load more: run syntax highlighter +- MySQL: Fix saving empty enum (bug #1152) - MySQL 5.0-: Do not load partitioning info in alter table (bug #1099) - MariaDB: Parse COLLATE in routine definition (bug #1104) - PostgreSQL: Show structure of inherited tables diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index 316720ce..f4dca8e2 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -717,13 +717,13 @@ class Adminer { * @param ?string $table null in call.inc.php * @param Field $field * @param string $attrs attributes to use inside the tag + * @param string|string[]|false|null $value false means original value * @return string custom input field or empty string for default */ - function editInput(?string $table, array $field, string $attrs, ?string $value): string { + function editInput(?string $table, array $field, string $attrs, $value): string { if ($field["type"] == "enum") { - return (isset($_GET["select"]) ? " " : "") - . ($field["null"] ? " " : "") - . enum_input("radio", $attrs, $field, $value, $value === 0 ? 0 : null) // 0 - empty value + return (isset($_GET["select"]) ? " " : "") + . enum_input("radio", $attrs, $field, $value, "NULL") ; } return ""; diff --git a/adminer/include/html.inc.php b/adminer/include/html.inc.php index 2745c3f0..1b0c4cbd 100644 --- a/adminer/include/html.inc.php +++ b/adminer/include/html.inc.php @@ -179,15 +179,17 @@ function hidden_fields_get(): void { /** Print enum or set input field * @param 'radio'|'checkbox' $type * @param Field $field -* @param mixed $value string|array +* @param string|string[]|false|null $value false means original value */ -function enum_input(string $type, string $attrs, array $field, $value, ?string $empty = null): string { +function enum_input(string $type, string $attrs, array $field, $value, string $empty = ""): string { preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); - $return = ($empty !== null ? "" : ""); - foreach ($matches[1] as $i => $val) { + $prefix = ($field["type"] == "enum" ? "val-" : ""); + $checked = (is_array($value) ? in_array("null", $value) : $value === null); + $return = ($field["null"] && $prefix ? "" : ""); + foreach ($matches[1] as $val) { $val = stripcslashes(str_replace("''", "'", $val)); - $checked = (is_array($value) ? in_array($val, $value) : $value === $val); - $return .= " '; + $checked = (is_array($value) ? in_array($prefix . $val, $value) : $value === $val); + $return .= " '; } return $return; } @@ -208,13 +210,13 @@ function input(array $field, $value, ?string $function, ?bool $autofocus = false $function = null; } $functions = (isset($_GET["select"]) || $reset ? array("orig" => lang('original')) : array()) + adminer()->editFunctions($field); - $disabled = stripos($field["default"], "GENERATED ALWAYS AS ") === 0 ? " disabled=''" : ""; - $attrs = " name='fields[$name]'$disabled" . ($autofocus ? " autofocus" : ""); $enums = driver()->enumLength($field); if ($enums) { $field["type"] = "enum"; $field["length"] = $enums; } + $disabled = stripos($field["default"], "GENERATED ALWAYS AS ") === 0 ? " disabled=''" : ""; + $attrs = " name='fields[$name]" . ($field["type"] == "enum" || $field["type"] == "set" ? "[]" : "") . "'$disabled" . ($autofocus ? " autofocus" : ""); echo driver()->unconvertFunction($field) . " "; $table = $_GET["edit"] ?: $_GET["select"]; if ($field["type"] == "enum") { @@ -234,12 +236,7 @@ function input(array $field, $value, ?string $function, ?bool $autofocus = false echo "" . ""; } elseif ($field["type"] == "set") { - preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); - foreach ($matches[1] as $i => $val) { - $val = stripcslashes(str_replace("''", "'", $val)); - $checked = in_array($val, explode(",", $value), true); - echo " '; - } + echo enum_input("checkbox", $attrs, $field, (is_string($value) ? explode(",", $value) : $value)); } elseif (is_blob($field) && ini_bool("file_uploads")) { echo ""; } elseif ($function == "json" || preg_match('~^jsonb?$~', $field["type"])) { @@ -297,12 +294,14 @@ function process_input(array $field) { $function = idx($_POST["function"], $idf); $value = idx($_POST["fields"], $idf); if ($field["type"] == "enum" || driver()->enumLength($field)) { - if ($value == -1) { + $value = $value[0]; + if ($value == "orig") { return false; } - if ($value == "") { + if ($value == "null") { return "NULL"; } + $value = substr($value, 4); // 4 - strlen("val-") } if ($field["auto_increment"] && $value == "") { return null; diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 16c50cc4..1657f30e 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -217,7 +217,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row } function selectVal($val, $link, $field, $original) { - $return = $val; + $return = "$val"; $link = h($link); if (is_blob($field) && !is_utf8($val)) { $return = lang('%d byte(s)', strlen($original)); @@ -264,14 +264,14 @@ ORDER BY ORDINAL_POSITION", null, "") as $row $fields = fields($_GET["select"]); foreach ($columns as $name => $desc) { $field = $fields[$name]; - if (preg_match("~enum~", $field["type"]) || like_bool($field)) { //! set - uses 1 << $i and FIND_IN_SET() + if ($field["type"] == "enum" || like_bool($field)) { //! set - uses 1 << $i and FIND_IN_SET() $key = $keys[$name]; $i--; echo "