Insert from for loop - add select option value to only one record
<p>I have an insert happening in a for loop inserting 4 records into database.  I want to have only one of these new records get the value from a select menu in the form, contained in the variable $action. The menu appears only for one record on the form, depending on user selections.</p>

<p>Table structure is:</p>

<pre><code>CREATE TABLE IF NOT EXISTS `pathway` (
  `pathway_pk` int(3) NOT NULL AUTO_INCREMENT,
  `case_fk` int(3) NOT NULL,
  `level` int(1) NOT NULL,
  `pathway_allowed` char(1) NOT NULL DEFAULT 'n',
  `pathway_action_fk` int(2) NOT NULL DEFAULT '0',
  `comment` text NOT NULL,
  `created` datetime NOT NULL,
  `created_by` int(3) NOT NULL,
  `updated` datetime NOT NULL,
  `updated_by` int(3) NOT NULL,
  PRIMARY KEY (`pathway_pk`)
)
</code></pre>

<p>The PHP:</p>

<pre><code>$author_pk = $_GET['author_pk'];
$case_pk = $_GET['case_pk'];
$patient_pk = $_GET['patient_pk'];

$pathway_allowed = intval($_POST['allowed']);

$action = mysql_real_escape_string($_POST['actions']);


if(isset($_POST['submit'])){ 

$pathway_comment = array();
foreach($_POST['comment'] as $comment) {
    $pathway_comment[]= mysql_real_escape_string($comment);
}
for($i=0, $count = count($pathway_comment);$i&lt;$count;$i++) {
    $comment = $pathway_comment[$i];
    $query = sprintf(
        "INSERT INTO pathway (             
           pathway_pk,
           case_fk,
           level,
           pathway_action_fk,
           pathway_allowed,
           comment
        ) VALUES (
           '',
           '$case_pk',
           '1',
           '$action',
           '%s',
           '$comment')", $pathway_allowed === $i ? 'y' : 'n');

       $result = mysql_query($query, $connection) or die(mysql_error());
</code></pre>

<p>The rest:</p>

<pre><code>&lt;script type="text/javascript"&gt;
$(document).ready(function() {
$("#jMenu").jMenu();
$("select, input, a.button, button").uniform();


$("input:radio[id='allowed_0']").click(function() { 

$("#required_p2").remove();
$("#required_p3").remove();
$("#required_p4").remove();

if($(".p1_action").text().trim()==""){
$(".p1_action").append('&lt;div class="required" id="required_p1"&gt;&lt;select name="actions" id="actions"&gt;&lt;option&gt;Select an action for this pathway...&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i&lt; len; i++) {
        html += '&lt;option value="' + data[i].pathway_action_pk + '"&gt;' + data[i].action + '&lt;/option&gt;';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});


$("input:radio[id='allowed_1']").click(function() { 

$("#required_p1").remove();
$("#required_p3").remove();
$("#required_p4").remove();

if($(".p2_action").text().trim()==""){
$(".p2_action").append('&lt;div class="required" id="required_p2"&gt;&lt;select name="actions" id="actions"&gt;&lt;option&gt;Select an action for this pathway...&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i&lt; len; i++) {
        html += '&lt;option value="' + data[i].pathway_action_pk + '"&gt;' + data[i].action + '&lt;/option&gt;';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});



$("input:radio[id='allowed_2']").click(function() { 

$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p4").remove();

if($(".p3_action").text().trim()==""){
$(".p3_action").append('&lt;div class="required" id="required_p3"&gt;&lt;select name="actions" id="actions"&gt;&lt;option&gt;Select an action for this pathway...&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i&lt; len; i++) {
        html += '&lt;option value="' + data[i].pathway_action_pk + '"&gt;' + data[i].action + '&lt;/option&gt;';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});



$("input:radio[id='allowed_3']").click(function() { 

$("#required_p1").remove();
$("#required_p2").remove();
$("#required_p3").remove();

if($(".p4_action").text().trim()==""){
$(".p4_action").append('&lt;div class="required" id="required_p4"&gt;&lt;select name="actions" id="actions"&gt;&lt;option&gt;Select an action for this pathway...&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;');
}

$.getJSON('../scripts/get_pathway_actions.php', function(data){
    var html = '';
    var len = data.length;
    for (var i = 0; i&lt; len; i++) {
        html += '&lt;option value="' + data[i].pathway_action_pk + '"&gt;' + data[i].action + '&lt;/option&gt;';
    }
    if($('select#actions option').length == 1){
    $('#actions').append(html);
    }
});
});


});

function validate(){
valid = true;

if ( document.form.comment_1.value == ""){
    alert ( "Please enter a comment for Pathway 1" );
    valid = false;
    return valid;
}

if ( document.form.comment_2.value == ""){
    alert ( "Please enter a comment for Pathway 2" );
    valid = false;
    return valid;
}

if ( document.form.comment_3.value == ""){
    alert ( "Please enter a comment for Pathway 3" );
    valid = false;
    return valid;
}

if ( document.form.comment_4.value == ""){
    alert ( "Please enter a comment for Pathway 4" );
    valid = false;
    return valid;
}




return valid;
}

function delayer(){
window.location = "create_pathways.php?author_pk=&lt;?php echo $author_pk; ?&gt;&amp;case_pk=&lt;?php echo $case_pk; ?&gt;&amp;patient_pk=&lt;?php echo $patient_pk; ?&gt;"
}
&lt;/script&gt;
&lt;body&gt;
&lt;?php include('menu_bar.php'); ?&gt;
&lt;div class="settings"&gt;
  &lt;div class="buttonn"&gt; &lt;a href="&lt;?php echo $_SERVER['PHP_SELF'], '?logout=true' ?&gt;" class="buttonn"&gt;Logout&lt;/a&gt; &lt;/div&gt;
  &lt;div class="buttonn"&gt; &lt;a href="change_password.php?author_pk=&lt;?php echo $author_pk; ?&gt;" class="buttonn"&gt;Change Password&lt;/a&gt; &lt;/div&gt;
&lt;/div&gt;
&lt;div class="title"&gt;Create Pathways for Level 1&lt;span class="message"&gt;&lt;?php echo $message; ?&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="top_form"&gt;
  &lt;form action="" method="post" name="form" id="form" onSubmit="return validate();"&gt;
    &lt;table width="182%" border="0" cellpadding="5"&gt;

      &lt;tr&gt;
        &lt;td valign="top"&gt;&lt;strong&gt;Pathway&lt;/strong&gt;&lt;/td&gt;
        &lt;td align="center" valign="top"&gt;&lt;strong&gt;Allowed&lt;/strong&gt;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;strong&gt;Blocking or Accepting Comment&lt;/strong&gt;&lt;/td&gt;
        &lt;td valign="top"&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td width="23%" valign="top"&gt;&lt;strong&gt;Pathway 1:&lt;/strong&gt; No action - See again&lt;/td&gt;
        &lt;td width="6%" align="center" valign="top"&gt;&lt;input type="radio" name="allowed" value="0" id="allowed_0" /&gt;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;div class="required"&gt;
            &lt;textarea name="comment[]" id="comment_1" cols="35" rows="5"&gt;&lt;/textarea&gt;
          &lt;/div&gt;&lt;/td&gt;
        &lt;td width="33%" align="left" valign="top" class="p1_action"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td valign="top"&gt;&lt;strong&gt;Pathway 2:&lt;/strong&gt; See again&lt;/td&gt;
        &lt;td align="center" valign="top"&gt;&lt;input type="radio" name="allowed" value="1" id="allowed_1" /&gt;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;div class="required"&gt;
            &lt;textarea name="comment[]" id="comment_2" cols="35" rows="5"&gt;&lt;/textarea&gt;
          &lt;/div&gt;&lt;/td&gt;
        &lt;td width="33%" align="left" valign="top" class="p2_action"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td valign="top"&gt;&lt;strong&gt;Pathway 3:&lt;/strong&gt; Refer to ED&lt;/td&gt;
        &lt;td align="center" valign="top"&gt;&lt;input type="radio" name="allowed" value="2" id="allowed_2" /&gt;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;div class="required"&gt;
            &lt;textarea name="comment[]" id="comment_3" cols="35" rows="5"&gt;&lt;/textarea&gt;
          &lt;/div&gt;&lt;/td&gt;
        &lt;td width="33%" align="left" valign="top" class="p3_action"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td valign="top"&gt;&lt;strong&gt;Pathway 4:&lt;/strong&gt; Refer to Specialist&lt;/td&gt;
        &lt;td align="center" valign="top"&gt;&lt;input type="radio" name="allowed" value="3" id="allowed_3" /&gt;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;div class="required"&gt;
            &lt;textarea name="comment[]" id="comment_4" cols="35" rows="5"&gt;&lt;/textarea&gt;
          &lt;/div&gt;&lt;/td&gt;
        &lt;td width="33%" align="left" valign="top"  class="p4_action"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td valign="top"&gt;&amp;nbsp;&lt;/td&gt;
        &lt;td align="center" valign="top"&gt;&amp;nbsp;&lt;/td&gt;
        &lt;td colspan="2"&gt;&lt;input type="submit" name="submit" id="submit" value="Create Pathways for Level 1" /&gt;&lt;/td&gt;
        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;

    &lt;/table&gt;
  &lt;/form&gt;
</code></pre>
