{"id":258,"date":"2026-01-22T01:31:14","date_gmt":"2026-01-22T01:31:14","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=258"},"modified":"2026-01-22T01:31:14","modified_gmt":"2026-01-22T01:31:14","slug":"loop-through-mysql-fields-and-output-a-form","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/loop-through-mysql-fields-and-output-a-form\/","title":{"rendered":"Loop Through MySQL Fields And Output A Form"},"content":{"rendered":"<p>This script will loop though all fields in a database and output the name and create a form field with appropriate record value.<\/p>\n<p>This is useful if you don&#8217;t want to manually code the form, or don&#8217;t know all of the field names<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n\/\/ Assumes you have your database connected here using variable $conn\r\n\r\n\/\/ Set a flag to determin if you're editing here by checking $_GET for example\r\n$editing = true;\r\n\r\nif ($editing) {\r\n    \/\/ Fetch the record to edit\r\n    $result = $conn-&gt;query(\"SELECT * FROM `$table` WHERE ID = $id\");\r\n    $row = $result-&gt;fetch_assoc();\r\n}\r\n\r\n\/\/ Grab the column names for your table\r\n$result = $conn-&gt;query(\"SHOW COLUMNS FROM `$table`\");\r\n$columns = $result-&gt;fetch_all(MYSQLI_ASSOC);\r\n\r\n\/\/ Loop through each column and work out its name and type\r\n\/\/ Then output the appropriate form field for the data type\r\nforeach ($columns as $column): ?&gt;\r\n        &lt;?php\r\n        $fieldName = $column['Field'];\r\n        $fieldType = $column['Type'];\r\n        \/\/ Skip auto-increment fields (like 'id')\r\n        if ($fieldName == 'WebsiteID') continue;\r\n        $value = $editing &amp;&amp; isset($row[$fieldName]) ? htmlspecialchars($row[$fieldName]) : '';\r\n        ?&gt;\r\n        &lt;div class=\"WebsiteField\"&gt;\r\n            &lt;label for=\"&lt;?php echo $fieldName; ?&gt;\"&gt;\r\n                &lt;?php echo ucfirst(str_replace('_', ' ', $fieldName)); ?&gt;\r\n            &lt;\/label&gt;\r\n            &lt;?php if (strpos($fieldType, 'enum') !== false): ?&gt;\r\n                &lt;!-- Handle ENUM fields as dropdowns --&gt;\r\n                &lt;?php\r\n                preg_match(\"\/^enum\\('(.*)'\\)$\/\", $fieldType, $matches);\r\n                $enumValues = explode(\"','\", $matches[1]);\r\n                ?&gt;\r\n                &lt;select name=\"&lt;?php echo $fieldName; ?&gt;\" id=\"&lt;?php echo $fieldName; ?&gt;\"&gt;\r\n                    &lt;?php foreach ($enumValues as $enumValue): ?&gt;\r\n                        &lt;option value=\"&lt;?php echo $enumValue; ?&gt;\" &lt;?php echo ($value == $enumValue) ? 'selected' : ''; ?&gt;&gt;\r\n                            &lt;?php echo $enumValue; ?&gt;\r\n                        &lt;\/option&gt;\r\n                    &lt;?php endforeach; ?&gt;\r\n                &lt;\/select&gt;\r\n            &lt;?php elseif (strpos($fieldType, 'text') !== false): ?&gt;   \r\n              &lt;textarea&gt;&lt;\/textarea&gt;&lt;br&gt;\r\n            &lt;?php else: ?&gt;\r\n                &lt;!-- Default to text input --&gt;\r\n                &lt;input type=\"text\" name=\"&lt;?php echo $fieldName; ?&gt;\" id=\"&lt;?php echo $fieldName; ?&gt;\" value=\"&lt;?php echo $value; ?&gt;\"&gt;\r\n            &lt;?php endif; ?&gt;\r\n            &lt;div class=\"ClearFix\"&gt;&lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n            \r\n      &lt;?php\r\n    endforeach; \r\n    \/\/ Tidy Up The Connection\r\n    $conn-&gt;close();\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This script will loop though all fields in a database and output the name and create a form field with appropriate record value. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,4],"tags":[31,66,24,9],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-mysql","category-php","tag-database","tag-forms","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}