Jump to content

Recommended Posts

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]
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.