Page 1 of 1

Completely dynamic database form..

Posted: Sun Aug 24, 2008 10:33 am
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');
 

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 1:59 pm
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.

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 4:05 pm
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!!

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 4:35 pm
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!

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 5:35 pm
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.

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 6:28 pm
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..

Re: Completely dynamic database form..

Posted: Sun Aug 24, 2008 9:22 pm
by Doug G
Google for php datagrid and you'll find some php apps that do what you're asking for.