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());
    }
?>

mysql_insert_id() gives you the ID of the most recent INSERT command you run.
In your code, you get the qid only once after you finish inserting all the qtitles, this is why you only get the qid of the last qtitle.

You should do the mysql_insert_id() after each INSERT, within the qtitle foreach, and nest the atitle foreach in the qtitle for each (after the mysql_insert_id)

i do understand wat u mean shlomia.. but a bit confused on how to alter the code.. i would appericiate ur help if u willing to show me hw to alter the code.. thanks in advance.

i do understand wat u mean shlomia.. but a bit confused on how to alter the code.. i would appericiate ur help if u willing to show me hw to alter the code.. thanks in advance.

The code should be something like this.
with an exception - the following code will insert all 6 answers for both questions. It's up to you to decide which answer should go with which question and handle this within the answers foreach.

<?php
 foreach ($_POST['questions'] as $q) 
 {
	if (trim($q) != '') 
	{
		$qtitles[] = $q;
	}
 }
 
 foreach ($_POST['options'] as $o) 
 {
	if (trim($o) != '') 
	{
		$atitles[] = $o;
	}
 }
  
 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();
	
	foreach ($atitles as $atitle) 
	{
		$a_query = "INSERT INTO answers (qid, atitle, acount) VALUES ('$qid', '$atitle', '0')";
		$a_result = mysql_query($a_query) or die("ERROR: $a_query. ".mysql_error());
	}
 }
  
 unset($query);
 unset ($result);
 unset($a_query);
 unset ($a_result);
 
 ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.