Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:07:30+00:00 2026-06-07T18:07:30+00:00

I’m facing a big problem here with PHP and I need your help. I’m

  • 0

I’m facing a big problem here with PHP and I need your help. I’m creating questions in PHP with alternatives. Each question will have 4 alternatives. Questions will be inserted in a table called questions and all alternatives will be inserted in another table which is called alternatives. Every questions has its own id and even every alternative will have own id too. But every group of alternatives will have question id’s too , to know which alternative belongs to which question. And then I will select them as radio buttons.
For example if we have two questions like

  1. What’s the name of Australian capital city? (Let’s say this question have id nr 1)
    A) Sidney
    B) New Delhi
    C) Canberra
    D) Maimi
    And All those alternatives above will have their own id’s and questions id with is 1 in this case.

  2. What is 89 + (3*9) (Let’s say this question have id nr 3)
    A) 116
    B) 117
    C) 115
    D) 112

And All those alternatives above will have their own id’s and questions id with is 3 in this case.

3. An object is dropped from the top of a 100-m-high tower. Its height aboveground after t seconds is 100-4.9t2 m. how fast is it failing 2 sec after is it dropped?
A) 98m
B) 78m
C) 56m
D) None of them
And so on …..

Here is my code:

if (isset($_REQUEST['submit']) && $_REQUEST['submit'] != "") {
    $questions = $_POST['questions']; 

    $alternatives = $_POST['alternatives']; 


    if(isset($_POST['questions']) && $_POST['questions'] != '')
    {

        foreach($questions as $q) {

            if($q !='') {

                $sql1 = " INSERT INTO questions (question) ".
                        " VALUES ('".$q."'); ";
                if (!db_query($sql1)) 
                    $errors['err']='Unable to create question';
                $question_id[] = mysql_insert_id(); 
            }

        }

        foreach($question_id AS $q_id) { // I think here is the problem but I don't know how to solv'

            foreach($alternatives as $alt){


                if($alt !=''){
                    $sql =  " INSERT INTO alternatives (alternative, question_id) ".
                                " VALUES ('".$alt."', ".$q_id."); ";
                                if (!db_query($sql))
                    $errors['err']='Unable to create alternatives';
                 }

            }

        }

    }

}




<form action="test.php" method="POST" enctype="multipart/form-data"><br /><br /><br />
for($i=1; $i<=10; ++$i){
$i.") "?><input type="text" name="questions[]" value="" size="100"/><br />
<br /><p>Alternatives:</p><br /> 
for($j=1; $j<=4; ++$j){
<input type="text" name="alternatives[]" value="" /><br /> } }
<input type="submit" name="submit" value="Send"></input>
 <input class="button" type="reset" value="Reset" />

</form>

Sorry for my code.. it was very difficult to write on here. But what is happening is :

All the 12 alternatives are inserted three times and each time they get all the same question_id. I mean the first 12 alternatives get question_id 1 , then those 12 alternatives are inserted again and they get question_id = 2 , and so on…. What I want is the first four alternatives get question_id 1, the alternatives of the second question would have question_id nr 2 and so on. I don’t know how to solve it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:07:33+00:00Added an answer on June 7, 2026 at 6:07 pm

    Why don’t you add the question number to the name of the input as a key:

    <form action="test.php" method="POST" enctype="multipart/form-data"><br /><br /><br />
    <?php 
    for($i=1; $i<=10; ++$i){?>
    <input type="text" name="questions[<?php echo $i;?>]" value="" size="100"/><br />
    <br /><p>Alternatives:</p><br /> 
    <?php for($j=1; $j<=4; ++$j){?>
    <input type="text" name="alternatives[<?php echo $i;?>][]" value="" /><br />
    <?php } } ?>
    <input type="submit" name="submit" value="Send"></input>
     <input class="button" type="reset" value="Reset" />
    
    </form>
    

    that way you’ll end with two very easy to loop arrays such as

      ["questions"]=>
      array(10) {
        [1]=>
        string(4) "What's the name of Australian capital city?"
        [2]=> ....
    
      ["alternatives"]=>
      array(10) {
        [1]=>
        array(4) {
          [0]=>
          string(6) "Sidney"
          [1]=>
          string(4) " New Delhi"
          [2]=>
          string(4) " Canberra"
          [3]=>
          string(2) "Maimi"
        }
        [2]=>
        array(4) {... and so on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.