Completely dynamic database form..

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
Scooby
Forum Newbie
Posts: 4
Joined: Sun Aug 24, 2008 10:26 am

Completely dynamic database form..

Post by Scooby »

I have been searching, for quite some time now, for any information/tutorials on how to create a completely dynamic web form.. This form will pull the fields out of the database and display them on the page, then if the user wishes, they may edit any of the fields or add/delete fields from the form..

Anybody out there seen a tutorial on this, or at least get me pointed in the right direction.. I have created this database table which holds all the field information..

Code: Select all

 
DROP TABLE IF EXISTS `dw_fields`;
CREATE TABLE `dw_fields` (
  `field_id` INTEGER unsigned NOT NULL auto_increment,
  `field_category` varchar(255) NOT NULL default '',
  `field_name` text NOT NULL,
  PRIMARY KEY  (`field_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
 
-- 
-- Dumping data for table `dw_fields`
-- 
 
INSERT INTO dw_fields VALUES(1, 'Customer Information', 'Bride');
INSERT INTO dw_fields VALUES(2, 'Customer Information', 'Groom');
INSERT INTO dw_fields VALUES(3, 'Customer Information', 'Address');
INSERT INTO dw_fields VALUES(4, 'Customer Information', 'City');
INSERT INTO dw_fields VALUES(5, 'Customer Information', 'State/Province');
INSERT INTO dw_fields VALUES(6, 'Customer Information', 'Zip/Postal Code');
INSERT INTO dw_fields VALUES(7, 'Customer Information', 'Phone Number');
INSERT INTO dw_fields VALUES(8, 'Customer Information', 'Email Address');
 
INSERT INTO dw_fields VALUES(9, 'Wedding Information', 'Wedding Date');
INSERT INTO dw_fields VALUES(10, 'Wedding Information', 'Approximate Time');
INSERT INTO dw_fields VALUES(11, 'Wedding Information', 'Location');
INSERT INTO dw_fields VALUES(12, 'Wedding Information', 'Reception');
INSERT INTO dw_fields VALUES(13, 'Wedding Information', 'Color Scheme (if applicable)');
INSERT INTO dw_fields VALUES(14, 'Wedding Information', 'Approximate Number Of Guests');
INSERT INTO dw_fields VALUES(15, 'Wedding Information', 'Type Of Ceremony');
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Completely dynamic database form..

Post by califdon »

I'm not sure I understand what you are trying to do. Do you want a web interface to a database, to allow adding/deleting/modifying records of data, or are you trying to allow a web user to change the structure of database tables by adding and deleting fields?? If it's the former, that's just standard web database operation. If it's the latter, not only is it very risky, it's asking for a complete database maintenance interface. I would certainly recommend using phpMyAdmin or similar existing package, rather than trying to write your own.
Scooby
Forum Newbie
Posts: 4
Joined: Sun Aug 24, 2008 10:26 am

Re: Completely dynamic database form..

Post by Scooby »

Well they will be allowed to do both.. They can edit the data in the database as well as simply adding or deleting a field on a table..

Here is where I'm at thus far..

Code: Select all

 
<?php
$query_customers_columns = "SHOW COLUMNS FROM dw_customers ";
$result_customers_columns = mysql_query($query_customers_columns) or die(mysql_error());
while ($row_customers_columns = mysql_fetch_object($result_customers_columns)) {
    $customer_columns[] = $row_customers_columns->Field;
}
foreach ($_POST['customers'] as $customer_field_data => $customer_data) {
    $customer_info[] = "'".$customer_data."'";
}
$query_customers  = "INSERT INTO dw_customers (".str_replace("customer_id,","",implode(",",$customer_columns)).") "; 
$query_customers .= "VALUES (".implode(",",$customer_info).") ";
//mysql_query($query_customers) or die(mysql_error());
echo $query_customers;
?>
 
And the query echo seems to match up so far:

Code: Select all

 
INSERT INTO dw_customers (customer_bride,customer_groom,customer_address,customer_city,customer_state,customer_zip,customer_phone,customer_email) VALUES ('Bride Name','Groom Name','Street Address','City ','State','Zip','555-345-2345','test@whatever.com') 
 
I'm still looking around for anything that could go wrong with this, but I am not seeing anything as of yet.. Any suggestions would be awesome!!
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Completely dynamic database form..

Post by Bill H »

I would echo califdon's concern about it being risky to allow users to add/delete fields to the database. Sort of like lending someone my car and saying that, sure, they can add and remove parts if they want to. Who knows what I'd get back when they are done. It might be something no longer even recognizable as a car!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Completely dynamic database form..

Post by califdon »

So you don't care about somebody (accidentally or on purpose) deleting the primary key of the table, or deleting all the records in the table? I think you would do well to explain to us why you would want to do this.
Scooby
Forum Newbie
Posts: 4
Joined: Sun Aug 24, 2008 10:26 am

Re: Completely dynamic database form..

Post by Scooby »

I just don't give them access to delete the primary key.. I only display the fields to them that don't matter if they get deleted or not.. But that was just my thinking.. I have just decided to not make those fields editable.. They probably won't need to be edited at all anyways.. You guys talked me out of it.. haha Thanks for the info..
Doug G
Forum Contributor
Posts: 282
Joined: Sun Sep 09, 2007 6:27 pm

Re: Completely dynamic database form..

Post by Doug G »

Google for php datagrid and you'll find some php apps that do what you're asking for.
Post Reply