给单选按钮php赋值并保存到sql

I have a little problem that i have been struggling for the last 16hrs I can find a solution for this... I really really need some help please...

I have the form which is generated by a query based on users ID...

ID-1 Question 1
 rad-btn ID-1 group 1 value 0 answer x
 rad-btn ID-2 group 1 value 0 answer x
 rad-btn ID-3 group 1 value 0 answer x

ID-2 Question 2
 rad-btn ID-4 group 2 value 0 answer x
 rad-btn ID-5 group 2 value 0 answer x
 rad-btn ID-6 group 2 value 0 answer x

ID-4 Question 3
 rad-btn ID-6 group 4 value 0 answer x
 rad-btn ID-7 group 4 value 0 answer x
 rad-btn ID-8 group 4 value 0 answer x

to get that "form" I use an sql as follow

$padreQR = mysql_query("SELECT * FROM ph_sec_preguntas WHERE s_codigo = '$secc1' ");

while ($arrayP = mysql_fetch_array($padreQR)) {
    $preguntasPP = $arrayP['pregunta_id'];
    $preguntass = mysql_query("SELECT * FROM preguntas_p WHERE question_id = '$preguntasPP' ");
    while ($q1 = mysql_fetch_array($preguntass)) {
        $qid = $q1['question_id'];
        $pp1 = $q1['question'];
        $er = $qid;
        echo '"<h3>' . $pp1 . '</h3>'; // Ya tenemos las preguntas segun el usuario
        $rrr = mysql_query("SELECT * FROM respuestas WHERE question_id = '$qid' ");
        while ($arrayRR = mysql_fetch_array($rrr)) {
            $respuestas = $arrayRR['answer'];
            $correcto = $arrayRR['correct'];


            echo '<label style="cursor:pointer;"><input type="radio" name="' . $er . '" value="' . $correcto . '">' . $respuestas . '</label>';
        }
    }
};

it may be not the best way to do it, for it works...

So what I need is to save this information but not all, I only need:

userID, questionID, and the answer that was selected

my problem is, how do I get the selected radio button? and get the ID from that answer, I don't need to know the value, just which radio button was select and so I can get the ID from that button, that way I can save it to the sql, the ID came from the SQL, the value will always be 1 or 0...

Right now I'm only showing 3 questions with multiple choices, but the idea is to have about 20 questions in a single page...

Thank you for taking the time to read my question...

Simply set each Value to ID, and You will know from $_POST array what options have been marked.

Something like this:

echo '<label style="cursor:pointer;"><input type="radio" name="preguntas[' . $pregunta_id . ']" value="' . $respuesta_id . '">' . $respuestas . '</label>';

And then when posting this to Your php script, you will get an array of answers.

foreach($_POST['preguntas'] as $pregunta_id => $respuesta_id){
   echo "RESPUESTA A PREGUNTA NUMERO $pregunta_id ERA $respuesta_id <br />
";
}

Something like that...

Lets say you have this form:

<form action="test.php">
    <input type="radio" name="quest" value="q1" /> Pregunta 1<br/>
            <input type="radio" name="ansq1" value="a1" /> Respuesta 1.1<br/>
            <input type="radio" name="ansq1" value="a2" /> Respuesta 1.2<br/>
            <input type="radio" name="ansq1" value="a3" /> Respuesta 1.3<br/>
            <br/>
    <input type="radio" name="quest" value="q2" /> Pregunta 2<br/>
            <input type="radio" name="ansq2" value="a1" /> Respuesta 2.1<br/>
            <input type="radio" name="ansq2" value="a2" /> Respuesta 2.2<br/>
            <input type="radio" name="ansq2" value="a3" /> Respuesta 2.3<br/>
            <br/>
    <input type="radio" name="quest" value="q3" /> Pregunta 3<br/>
            <input type="radio" name="ansq3" value="a1" /> Respuesta 3.1<br/>
            <input type="radio" name="ansq3" value="a2" /> Respuesta 3.2<br/>
            <input type="radio" name="ansq3" value="a3" /> Respuesta 3.3<br/>
    <input type="submit" value="submit" />
</form>

On test.php you will have something like this:

<?php 
$pregunta = $_GET['quest'];
$respuesta = $_GET['ans'.$pregunta]; //Add the question to the index to filter the answer.
    echo "Seleccionaste la pregunta: $pregunta y la respuesta: $respuesta";
?>

You can change the method of this form to POST if security needed.

Now, with those values you can easily save them to the DB. For that, I recommend you to learn about how prepared statements works. Also, mysql_* functions are deprecated, try to avoid them. Use PDO or MySQLi instead.