Jump to content

Dynamic Database Fields


Scooby08

Recommended Posts

I have a table in my database for all the field names that is editable, so I never know how many fields there will be.. You can add or delete rows..  I run my SELECT query to display them on the page, but I cannot think how I am supposed to write the INSERT INTO query when I don't know the how many there will be..

 

This INSERT INTO works right now for me, when I know what all the fields are, but if I add another field to the table this will not have that field in the INSERT INTO query..

 

<?php
$query_customers  = "INSERT INTO dw_customers (customer_name, customer_phone, customer_email) "; 
$query_customers .= "VALUES ('$_POST[customer_name]', '$_POST[customer_phone]', '$_POST[customer_email]') ";		
?>

 

How can that be written to insert all the fields information without knowing how many fields there will be, and do I need to create an array out of the fields like so:

 

<input type="text" name="customers[]" />
<input type="text" name="customers[]" />
<input type="text" name="customers[]" />

 

or do I need to do something like this:

 

<input type="text" name="customers_1" />
<input type="text" name="customers_2" />
<input type="text" name="customers_3" />

 

Any help getting me pointed into the right direction will be much appreciated.. Thanks..

Link to comment
Share on other sites

So, say your table (dw_customers) has those three fields (customer_name, customer_phone, customer_email) - If you add a forth (customer_telephone), you want your queries to automatically accommodate that addition?

 

If I have that right, it is probably doable but I HIGHLY recommend that you don't follow that train of thought.

 

You have two options:

  • Design the system before you build it, not while you build it
  • Design the database in a more efficient way

 

To design the database to work in a way where you can add fields, I'd probably tackle it like this:

 

customer

id, name

 

attributes

id, name

 

customer_attribute

id, customer, attribute, value

Link to comment
Share on other sites

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.