admin管理员组

文章数量:1315252

I am new to PHP, I am making a Quiz page. The answers in FORM are sent to test-submit.php as follow:

question1-answer1; question2-answer2; question3-answer2...

http://localhost/tracy/test-submit.php?q1=a1&q2=a1&q3=a2&q4=a1

So I am confused how to get all the data separately to import to SQL database?

  • q1 = a1
  • q2 = a2
  • q3 = a1
  • ...

What if I have 1000 questions in URL? Iam looking for any help. Really appreciated!!!

I tried with $_GET[''] but I don't know what to get. is it:

  • $_GET['q1'];
  • $_GET['q2'];
  • $_GET['q3'];
  • $_GET['q4'];
  • $_GET['q5'];
  • ...

I am new to PHP, I am making a Quiz page. The answers in FORM are sent to test-submit.php as follow:

question1-answer1; question2-answer2; question3-answer2...

http://localhost/tracy/test-submit.php?q1=a1&q2=a1&q3=a2&q4=a1

So I am confused how to get all the data separately to import to SQL database?

  • q1 = a1
  • q2 = a2
  • q3 = a1
  • ...

What if I have 1000 questions in URL? Iam looking for any help. Really appreciated!!!

I tried with $_GET[''] but I don't know what to get. is it:

  • $_GET['q1'];
  • $_GET['q2'];
  • $_GET['q3'];
  • $_GET['q4'];
  • $_GET['q5'];
  • ...
Share Improve this question edited Jan 30 at 9:01 AymDev 7,6094 gold badges33 silver badges58 bronze badges asked Jan 30 at 7:36 Tồ MạnhTồ Mạnh 151 silver badge4 bronze badges 6
  • 2 Yes that's how you access them. If you have a lot of data, consider a) sending the data via POST instead, and b) maybe sending it in array format, or perhaps even JSON format so it's easier to loop through – ADyson Commented Jan 30 at 7:37
  • 1 "What if I have 1000 questions in URL?" - you do know basics such as a for loop, and string concatenation - no? for ($i=1; $i<1001; ++$i) { do_stuff_with($_GET['q'.$i]); } – C3roe Commented Jan 30 at 7:46
  • 1 Or - name your form fields with the scheme name="q[question-id-goes-here]" - name="q[1]", name="q[2]", etc. Then PHP will provide $_GET['q'] as an array, that you can easily iterate over with a foreach loop. And then you will not even need to know the exact number of questions beforehand, or you could even have "gaps" in the question IDs. – C3roe Commented Jan 30 at 7:49
  • 1 "i don't know what to get." - why not try it out and see what happens? – Nico Haase Commented Jan 30 at 8:28
  • 1 You probably have the questions themselves already on the server, so there doesn't seem to be a need to sent the questions in as well? Only the answers will do as long as you know to which questions they belong. Also, consider not submitting all answers in one step. What if something goes wrong? All answers will be lost? Normally you would submit a few answers and then proceed to the next set of questions. – KIKO Software Commented Jan 30 at 8:29
 |  Show 1 more comment

1 Answer 1

Reset to default 0

You can loop through all the query params and check if the value corresponding to the key exists

foreach($_GET as $key => $val) {
    if(!empty($val)) {
        $save[$key] = $val;
    }
}

Then you can process the final $save array as you want.

More importantly, if you have 1000 questions, means large form value and doing database operations, you should use POST method instead of GET. In that case the loop will be similar:

foreach($_POST as $key => $val) {
    if(!empty($val)) {
        $save[$key] = $val;
    }
}

本文标签: phpGET multiple variable separated from POST formStack Overflow