Jump to content


Photo

Reset database fields to default


  • Please log in to reply
3 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 11 October 2006 - 07:48 PM

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?
~ Mutley.

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 October 2006 - 11:25 PM

Do you mean you want to display default information in the form, the default values from the column on the database?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 12 October 2006 - 07:10 AM

You could query the columns info and then use the results to set the values in a form.
Here is a start point:
from: http://dev.mysql.com...n/describe.html
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.
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       |                |
+------------+----------+------+-----+---------+----------------+


#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 12 October 2006 - 10:08 AM

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'.

<?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;
?>

Regards
Huggie

Edit: This code has been tested and works
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users