need help - foreach loop

Discussion in 'PHP' started by wayz1229, Oct 27, 2009.

  1. #1
    i need help with the code below.. the code does not get correct qid from table questions.

    let say i enter 2 qtitle, and each qtitle has 3 atitle..
    but once the data entered in database..

    the error is like below:

    (qid=1)qtitle1- (none of the atitle having qid1)

    (qid=2)qtitle2- atitle1, atitle2, atitle3, atitle4, atitle5, atitle6 (all the atitle having qid2)

    anyone can help me to solve this problem.. i realy need some help.. the error is on the coding below..
    <?php
    foreach ($_POST['questions'] as $q) {
            if (trim($q) != '') {
                $qtitles[] = $q;
            }
        }
    	
    foreach ($qtitles as $qtitle) {
            $query = "INSERT INTO questions (qtitle) VALUES ('$qtitle')";
            $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
        }
    	
        $qid = mysql_insert_id();
    
        unset($query);
        unset ($result);
    
    
    foreach ($_POST['options'] as $o) {
            if (trim($o) != '') {
                $atitles[] = $o;
            }
        }
    
        foreach ($atitles as $atitle) {
            $query = "INSERT INTO answers (qid, atitle, acount) VALUES ('$qid', '$atitle', '0')";
            $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
        }
    ?>
    
    Code (markup):

     
    wayz1229, Oct 27, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    please post html code from where the questions and options are generated.

    let us see how the grouping is done so that at php level we can identify atitle for respective qtitles.

    however, no matter what, there is a logical mistake in code which is that you are doing mysql_insert_id() only once after doing several inserts in questions table.

    question & answer inserts should be coupled. That means, once a question is inserted in questions table, respective answers should be inserted in answers table using qid of the question inserted.

    provide us with more info to help you more.
     
    mastermunj, Oct 27, 2009 IP
  3. wayz1229

    wayz1229 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this is the code for form.. i realy need help on this. thankyou.

    
    <form action = 'createsurvey.php' method ='post'>
    <table border = '0' cellspacing = '5'>
    <tr>
    <td width="76"><div align="left" class="style2">Survey Title</div></td>
    <td width="5" class="style2">:</td>
    <td width="255"><input name = 'stitle' type = 'text' size="38"></td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <?php
    $i = 1;
    while($i < 2){
    echo("<tr>
    <td width='76'><div align='left' class='style2'>Question ".$i."</div></td>
    <td width='5' class='style2'>:</td>
    <td width='255' class='style2'><input type='text' name='questions[]' size='38'></td>
    </tr>");
    $a = 1;
    while ($a < 3){
    echo("<tr>
    <td width='76'><div align='right' class='style3'>Option ".$a."</div></td>
    <td width='5' class='style3'>:</td>
    <td width='255' class='style3'><input type='text' name='options[]' size='20'></td>
    </tr>");
    $a++;
    }
    echo ("<tr>
    <td width='76'></td>
    <td width='5'></td>
    <td width='255'></td>
    </tr>");
    echo "<td><br></td>";
    $i++; //Add one to $i
    } //stop while
    ?>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td></td>
    <td colspan = '2' align = 'right'><input type = 'reset' name = 'Reset' value = 'Reset' />
    <input type = 'submit' name = 'submit' value = 'Finish' /></td>
    </tr>
    </table>
    </form>
    Code (markup):
     
    wayz1229, Oct 27, 2009 IP
  4. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The corrected code:

    <?php
    foreach ($_POST['questions'] as $q) {
            if (trim($q) != '') {
                $qtitles[] = $q;
            }
        }
    	
    foreach ($qtitles as $qtitle) {
            $query = "INSERT INTO questions (qtitle) VALUES ('$qtitle')";
            $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
        
    
        $qid = mysql_insert_id();
    
        unset($query);
        unset ($result);
    
    
    foreach ($_POST['options'] as $o) {
            if (trim($o) != '') {
                $atitles[] = $o;
            }
        }
    
        foreach ($atitles as $atitle) {
            $query = "INSERT INTO answers (qid, atitle, acount) VALUES ('$qid', '$atitle', '0')";
            $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
        }
    
    }
    	
    ?>
    Code (markup):
    Your loops were wrong, you should run the second insert block in side the first loop for it to work
     
    adwebtiser, Oct 28, 2009 IP
  5. wayz1229

    wayz1229 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Mr adwebtiser.. the corrected code from u is getting error like below:

    (qid=1)qtitle1- getting 6 atitle (by right should be 3 atitle)

    (qid=2)qtitle2- getting 12 atitle (by right should be 3 atitle)
     
    wayz1229, Nov 8, 2009 IP
  6. mraza

    mraza Peon

    Messages:
    75
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    unset the $atitles array as well

    unset($query);
    unset ($result);
    unset ($atitles);
     
    mraza, Nov 8, 2009 IP