SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create fields in a table in mysql dynamically

    Question:

    How to do insert a number of fields into a table from a form a user fills out?

    Using php here and mysql

    Example. User fills out a survey build form and wants 6 questions. Then clicks next. It now shows 6 blank fill out questions for the user to fill out. Once he is done he clicks finish. From there it creates the table and the fields based on the number of questions. So 6 questions equal 6 fields.

    so how do you dynamically build fields in a new table based on the user input from a form?

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would not do it like that. I would have a user table and a question table like this:

    user

    id | username
    ====================
    1 | sean
    2 | bob

    questions

    id | user | question
    ====================
    1 | 1 | What colour is the sun?
    2 | 1 | What colour is the sea?
    3 | 2 | Why are Sean's questions about colours?

    So, when a new user signs up create their entry in the user table and then you can add their questions to the question table based on their user id

    Hope that helps!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the questions will be seperated out by customer. A new customer needs to have the freedom of deciding how many questions they want. If they want 6 questions on this survey then they get it.

    So once they fill out all 6 questions it builds the table accordingly. The fields wont be named what the question is. It will be question_1 to the end of the questions.

    From there a fill out form is generated for their customers to fill out and the results pupulate into the fields accordingly.

    I hope I make sense here. hehe Cause im confusing myself. ;P

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, I agree with seanf... a page that automatically generates tables sounds like it could easily get out of hand.

    That aside, if you were asking about how to get a dynamic number of fields entered into a database consistantly:

    If the name of each text field is is followed by brackets all the results will be in an array.

    So you can generate a bunch of text boxes like this:
    PHP Code:
    <input type="text" name="color_question[]">
    <
    input type="text" name="color_question[]">
    <
    input type="text" name="color_question[]">
    <
    input type="text" name="color_question[]">
    <
    input type="text" name="color_question[]">
    <
    input type="text" name="color_question[]"
    And they will be in the array $color_question once they submit. Then you can step through the array entering each element into the database.


    Question 4 = "Why is Bob so worried about Sean's questions?"
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Sillysoft
    Well the questions will be seperated out by customer. A new customer needs to have the freedom of deciding how many questions they want. If they want 6 questions on this survey then they get it.

    So once they fill out all 6 questions it builds the table accordingly. The fields wont be named what the question is. It will be question_1 to the end of the questions.
    I agree with seanf - the solution to this problem is not to bulid a new table for each poll. I suggest you take a look at some tutorials on database normalization:

    http://www.phpbuilder.com/columns/barry20000731.php3
    http://www.devshed.com/Server_Side/MySQL/Normal


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •