Mutley Posted October 11, 2006 Share Posted October 11, 2006 In my SQL database I've set a default to every field.Is it possible in PHP to reset all the fields to these defaults using a form? Quote Link to comment https://forums.phpfreaks.com/topic/23697-reset-database-fields-to-default/ Share on other sites More sharing options...
HuggieBear Posted October 11, 2006 Share Posted October 11, 2006 Do you mean you want to display default information in the form, the default values from the column on the database?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23697-reset-database-fields-to-default/#findComment-107676 Share on other sites More sharing options...
Firemankurt Posted October 12, 2006 Share Posted October 12, 2006 You could query the columns info and then use the results to set the values in a form.Here is a start point:from: [url=http://dev.mysql.com/doc/refman/5.0/en/describe.html]http://dev.mysql.com/doc/refman/5.0/en/describe.html[/url] DESCRIBE provides information about the columns in a table. It is a shortcut for SHOW COLUMNS FROM. As of MySQL 5.0.1, these statements also display information for views. (See Section 13.5.4.3, “SHOW COLUMNS Syntax”.)col_name can be a column name, or a string containing the SQL ‘%’ and ‘_’ wildcard characters to obtain output only for the columns with names matching the string. There is no need to enclose the string within quotes unless it contains spaces or other special characters.[code]mysql> DESCRIBE city;+------------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+------------+----------+------+-----+---------+----------------+| Id | int(11) | NO | PRI | NULL | auto_increment || Name | char(35) | NO | | | || Country | char(3) | NO | UNI | | || District | char(20) | YES | MUL | | || Population | int(11) | NO | | 0 | |+------------+----------+------+-----+---------+----------------+[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23697-reset-database-fields-to-default/#findComment-107792 Share on other sites More sharing options...
HuggieBear Posted October 12, 2006 Share Posted October 12, 2006 Below is an example of getting the default value for each column from the database, putting it into an array and then using it in a form as the pre-filled value...This code assumes I have a table called 'people' that has two columns in it called 'gender' and 'title'. The default value for title is 'Mr' and the default value for gender is 'Male'.[code]<?php// Connect to databaseinclude_once('connect.php');// Select the column details from the table$sql = "DESCRIBE people";$result = mysql_query($sql);if (!$result){ echo "Couldn't execute <b>$sql</b>: " . mysql_error() . "\n";}// Put the default values for each column into an array keyed on column namewhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $columns[$row['Field']] = $row['Default'];}// Create the HTML formecho <<<HTML<html> <form name="name" action="process.php" method="GET"> <input type="text" name="title" value="{$columns['title']}"> <input type="text" name="gender" value="{$columns['gender']}"> <input type="submit" name="submit" value="Update"> </form></html>HTML;?>[/code]RegardsHuggie[size=8pt][color=red][b]Edit:[/b][/color] This code has been tested and works[/size] Quote Link to comment https://forums.phpfreaks.com/topic/23697-reset-database-fields-to-default/#findComment-107823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.