Insert Row and Update the same row In PHP / MYSQL simultaneously
<p>Please help me with this:</p>

<p>I am creating a survey facility for the administrator. The administrator is asked for the number of questions in the questionnaire. And based on this the rows are created in the survey details table. In the question table the corresponding number of rows are created for the same in the question table.</p>

<p>Now the admin can enter the questions and answers. For this I need to insert the row in question table and update it from the informations collected from FORM using a loop. But the inserted rows are not getting updated simultaneously. Help me with this or is there any other way to do this?</p>

<p>This is my code: Please ignore the programming style as this is the script by a novice:</p>

<pre><code>$sid = intval ($_GET['ids']);
$noq = intval ($_GET['qn']);
for($noq !=0;$noq &gt;=1;$noq--){  
    $q = "insert into sur_ques (sur_id) values ('$sid')";
    $ex = mysql_query($q);
    $rs = mysql_affected_rows();
    if($rs ==1){ echo" Questions Rows Created Corresponding to Survey Subject";}
?&gt;
&lt;form name="form1" method="post" action="&lt;?php echo($PHP_SELF); ?&gt;"&gt;
&lt;table width="500" border="0" cellspacing="1" cellpadding="0"&gt;
    &lt;tr&gt;&lt;br&gt;&lt;b&gt;Ques No-&lt;?php echo"$noq";?&gt;&lt;/b&gt;&lt;/tr&gt;&lt;br&gt;
    &lt;tr&gt;&lt;textarea name = "q" rows ="10" cols = "70" wrap = "hard" &gt;&lt;/textarea&gt;&lt;/tr&gt;&lt;br&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Ans 1:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="a1" size="37" /&gt;&lt;/td&gt;&lt;/tr&gt;   
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Ans 2:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="a2" size="37"  /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Ans 3:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="a3" size="37"  /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;b&gt;Ans 4:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="a4" size="37" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;input type = "submit" name="qa" Value = "Add Q&amp;A" /&gt;
&lt;input type ="reset" Value="Reset" /&gt;
&lt;/form&gt; 
&lt;?
    if ($_POST['qa']){
        $id = mysql_insert_id();
        $result = mysql_query("update ques set q_txt = '$q', ans1 = '$a1' ans2 = '$a2'  ans4 = '$a4' ans4 = '$a4' where q_id = '$id'");
        if($r = mysql_num_rows($result)){
            echo" Question and answers updated";
        }
    } else {
        break;
    }
}
?&gt;
</code></pre>
