简体   繁体   中英

How can i pass multiple radio button values to MYSQL Database via PHP as all individual values but keeping them segregated into groups?

I have a long questionnaire (46 questions) with each having (3) possible answers, yes, sometimes, and no, (3 radio buttons) each of which have a corresponding value, 10, 5, and -10 respectively.

I had the questions divided up amongst 4 headings with several questions under each heading. I was able to post all the data to mySQL Database by giving each a different name attribute that corresponded with its field in the Database.

The problem I am having is that I can no longer use the radio buttons as a set, that is, they are all selectable, defeating the purpose of having them, essentially making them check-boxes....

I need to have each radio button its own value in my database and I need them to be grouped in their 3 possible choices for each question...

I will post an example of my form html and php send script.... Thank you in advance for any advice...

html code one question

          <div class="questionBox">
       <p>1.Have I clearly defined my companies target market?</p>
        <input type="radio" name="ca1y" value="10"/>
          <label for="ca1">YES</label>
      <input type="radio" name="ca1s" value="5"/>
          <label for="ca1">SOMEWHAT</label>
      <input type="radio" name="ca1n" value="-10"/>
        <label for="ca1">NO</label>
        </div>

/* php post update */

    $radioPost="INSERT INTO questions 
(       ca1y, 
        ca1s, 
        ca1n,...)
VALUES
(   '$_POST[ca1y]',
    '$_POST[ca1s]',
    '$_POST[ca1n]',...)

I tried to give each radio button an ID attribute and use that to update the database but I can't seem to get that to work....

any help would be greatly appreciated cheers.

I'm not sure if I'm getting the jist of what you trying to do, but it seems like you need to save an individuals answers to different questions to the DB but that each question can only have ONE of let's say three possible answers. Well, in that case you might think of redesigning your DB to look something like this:

在此处输入图像描述

You store the users details in the users table, the question details in the questions table and the different answers in the answers table. A user will be able to answer multiple questions but can only answer a particular question once. You then save the answer in ONE field called answer_value for example. There is no need to store the answer in multiple fields since you will be storing the VALUE of the radio button they chose, ie you will store either a 10, 5 or -10 depending on which radio option was selected.

It's also important that in your HTML you name all the radio buttons for a particular question the same. This ensures that the radio options act as radio buttons and not as checkboxes. Your HTML would most likely look something like this:

<div class="questionBox">
   <p>1.Have I clearly defined my companies target market?</p>
    <input type="radio" name="q1" value="10"/>
      <label for="ca1">YES</label>
  <input type="radio" name="q1" value="5"/>
      <label for="ca1">SOMEWHAT</label>
  <input type="radio" name="q1" value="-10"/>
    <label for="ca1">NO</label>
</div>
<div class="questionBox">
   <p>2.Have I clearly defined my companies product?</p>
    <input type="radio" name="q2" value="10"/>
      <label for="ca2">YES</label>
  <input type="radio" name="q2" value="5"/>
      <label for="ca2">SOMEWHAT</label>
  <input type="radio" name="q2" value="-10"/>
    <label for="ca2">NO</label>
</div>
.....

Note that question 1 has 3 radio options all named "q1" and question 2 has 3 radio options all named "q2". Follow the same convention for all your questions. Your PHP then becomes pretty straightforward and will probably look something like this:

$radioPost="INSERT INTO answers (user_name, question_name, answer_value)
VALUES (('$_POST[user_name]', 'q1', '$_POST[q1]),('$_POST[user_name]', 'q2', '$_POST[q2]), ....)

If you not sure what the radio button names are, you can use a foreach loop as follows:

$user = $_POST[user_name];
foreach ( $_POST as $key => $val )
{   
    If ($key <> 'user_name') //you might need to check that you only reading the radio                  options
    {
       $radioPost="INSERT INTO answers (user_name, question_name, answer_value)
                   VALUES ('$user', $key, '$val')"
        ....
    }
}    

Also be advised that even if you only using radio options, you should still clean the inputs to prevent attacks like SQL-Injection etc. I suggest you find some more info on securing your code against attack.

BTW, if you place the questions in a table of it's own, you can dynamically create your questionnaire from this table, which then allows you to add or delete questions at will, making your whole application a lot more flexible. And saving your answers in one field instead of three, will remove all the NULL values from your table, which will simplify your queries when you perform calculations. I know MySQL normally puts a zero as the default integer value, but it's still better to save it into one field. If you want to know which option a user chose for a particular question, just check the value stored in that field.

Not very good solution: write js to deselect appropriate radio buttons.

On more serious note i don't understand the need to have all radios with different names. Why is such solution not applicable: name radios in one question with same name, lets say question1, then after form post just look at value and from that you can easily update db. I suppose you want to collect how many times each radio was selected. So if you know that question1 had choice 1 you than also know that question1 did not have choices 2 and 3. Unless you collect data in other way or for other purpose, but then I think there need to be a bit more input from you about the purpose of this. Though I still think you can do this like i said, this would not need complex processing, id say rudimentary programming knowledge should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM