Jump to content

Rookie needs help


csabi_fl

Recommended Posts

Hi.
I am building a website and I am stuck .
I have a form on one of my pages that has 5 fields(M,Tu,W,Th and F).Each field contains a question .Members have to answer each question in any particular order and one at a time.When they for instance answer question M and click on submit the answer goes into a table in my database.I use Mysql.Now the table has 6 columns :loginid,M,Tu,W,Th and F.
What I need is a code that inserts the data into the table one at a time and I want to have only one row,something like this:
loginid/answer to M/answer to Tu/answer to W/answer to Th/answer to F,
so everything is nice and neat.
And one more thing,I want to prevent them from sending in an answer to the same question twice.
Please help me out , I have been trying for about a month now with no success.
Thank you very much in advance for your time.
Link to comment
Share on other sites

Well, first, I would discourage storing each question/answer pair as a separate column/value, since this is quite inflexible.  However, as far as your UPDATE statement is concerned, you could easily make sure the column was blank to begin with.  Alternatively, if you were to normalize the table, you could simply run INSERT IGNOREs with a proper UNIQUE key.
Link to comment
Share on other sites

Sorry, my bad... instead of having multiple columns for each question, you should simply have loginid/question/answer as your columns.  That way, for each "answer", you simply INSERT a new row, with the loginid of the user, the question, and the answer value provided.  If you set up the table with a UNIQUE index on (loginid, question), then you won't be able to have multiple answers for a given question for a given user.  Hence INSERT IGNORE.  Does that make more sense?
Link to comment
Share on other sites

It is in the documentation for INSERT syntax.  Basically, if you were to try and insert a record that would result in a duplicate result for a UNIQUE index, you would normally get an error.  This can often be useful, if you want to notify the user, for instance (like for logins/e-mails); however, in your case, it sounds like you don't care, so you can simply supress the error, and no new record will be inserted.
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.