Jump to content


Photo

Rookie needs help


  • Please log in to reply
5 replies to this topic

#1 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 October 2006 - 06:51 PM

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.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 October 2006 - 07:48 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 October 2006 - 08:03 PM

I'm afraid I don't understand.I am not a pro,could you be a little more specific,PLEASE.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 October 2006 - 10:21 PM

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?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 csabi_fl

csabi_fl
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 05 October 2006 - 04:55 AM

It makes a lot of sense.I think it's an excellent idea.The only thing that I am not familiar with is the INSERT IGNORE command.I tried to look it up in the Mysql documentation but there isn't much there.Could you tell me how to use it in my case?

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 October 2006 - 06:54 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users