Jump to content

Archived

This topic is now archived and is closed to further replies.

Mutley

Reset database fields to default

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Do you mean you want to display default information in the form, the default values from the column on the database?

Regards
Huggie

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 database
include_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 name
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $columns[$row['Field']] = $row['Default'];
}

// Create the HTML form
echo <<<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]

Regards
Huggie

[size=8pt][color=red][b]Edit:[/b][/color] This code has been tested and works[/size]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.