Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MultiSelect SElection box and inserting MySQL query help

Status
Not open for further replies.

WizyWyg

Technical User
Jan 31, 2001
854
JP
Im in need of some assitance in order to implement a multiselect selection box and then inserting the choices as seperate records into a table in a MySql DB.

For the form, Im pulling information from two tables, then inserting it into a third as seperate records, based on what is chosen in the multiselect box.

From table 1 , Im pulling the last record inserted.
From table 2 , Im pulling in the memberid and membername (multiselect box)

For table 3, should insert whatever is selected in the multiselect box as seperate records from table 1.

Ie, if last record was #45
Multiselect box chosen were id's 3,4,6,9 (shown name of member, but only their id will be inserted into the table)
Table 3 would result in :
45 | 3
45 | 4
45 | 6
45 | 9


My multiselect box code looks like this:
Code:
<select size=10 multiple name=&quot;who&quot;>
<?php
	
  $q=&quot;select member_id, membername from members
  order by member_id asc&quot;;
  $r=mysql_query($q) or die(mysql_error().$q);
   while($row=mysql_fetch_array($r)) {
    echo &quot;<option value=\&quot;&quot;.$row[&quot;member_id&quot;].&quot;\&quot;>&quot;.$row[&quot;membername&quot;].&quot;</option>&quot;;
  }
  ?>
</select>

How can I construct the MySQL insert query so that it loops through the chosen selections and inserts them until all are inserted?

 
You need to name the select element as an array:
Code:
<input .... name=&quot;who[]&quot;>

When you POST your form there will be an array $_POST[who] ($HTTP_POST_VARS[who] before PHP 4.1.2).

I suggest:
Code:
if (is_array($_POST[who])){ # make sure something was selected
   # loop through all the selections
   foreach ($_POST as $key=>$value){
      # compose SQL here
      $SQL = &quot;INSERT INTO .......
      # execute the query
      ...
   }
}
 
Of course:
Code:
if (is_array($_POST[who])){ # make sure something was selected
   # loop through all the selections
   foreach ($_POST[who] as $key=>$value){
      # compose SQL here
      $SQL = &quot;INSERT INTO .......
      # execute the query
      ...
   }
}
 
Let me add to this, noticing that my question was somehow truncated during the post, so here is a more clearer explanation:

Im in need of some assitance in order to implement a multiselect selection box and then inserting the choices as seperate records into a table in a MySql DB.

For the form, Im pulling information from two tables, then inserting it into a third as seperate records, based on what is chosen in the multiselect box.

From table 1 , Im pulling the last record inserted.
From table 2 , Im pulling in the memberid and membername (multiselect box)

For table 3, should insert whatever is selected in the multiselect box as seperate records from table 1.

Ie, if last record was #45
Multiselect box chosen were id's 3,4,6,9 (shown name of member, but only their id will be inserted into the table)
Table 3 would result in :
45 | 3
45 | 4
45 | 6
45 | 9

Im currently modifying a code that only inserts 1 record at a time below is the code:

Code:
//===============================
// Record Form Queries
//-------------------------------
function participant_action($sAction)
{
  global $db;
  global $tpl;
  global $sForm;
  global $sparticipantErr;
  $bExecSQL = true;
  $sActionFileName = &quot;&quot;;
  $sParams = &quot;?&quot;;
  $sWhere = &quot;&quot;;
  $bErr = false;
  $pPKid = &quot;&quot;;
  $fldeventid = &quot;&quot;;
  $fldwho = &quot;&quot;;
// Event begin
  $sActionFileName = &quot;admin_event.php&quot;;
  $sParams .= &quot;eventid=&quot; . urlencode(get_param(&quot;Trn_eventid&quot;));

// CANCEL action
  if($sAction == &quot;cancel&quot;)
  {
 header(&quot;Location: &quot; . $sActionFileName . $sParams);
    exit;
  }

// Build WHERE statement
  if($sAction == &quot;update&quot; || $sAction == &quot;delete&quot;) 
  {
    $pPKid = get_param(&quot;PK_id&quot;);
    if( !strlen($pPKid)) return;
    $sWhere = &quot;id=&quot; . tosql($pPKid, &quot;Number&quot;);
  }


// Load all form fields into variables
  $fldeventid = get_param(&quot;eventid&quot;);
  $fldwho = get_param(&quot;who&quot;);

// Create SQL statement
  switch(strtolower($sAction)) 
  {
    case &quot;insert&quot;:
        $sSQL = &quot;insert into participants (&quot; . 
          &quot;eventid,&quot; . 
          &quot;who)&quot; . 
          &quot; values (&quot; . 
          tosql($fldeventid, &quot;Number&quot;) . &quot;,&quot; . 
          tosql($fldwho, &quot;Number&quot;) . 
          &quot;)&quot;;
    break;
    case &quot;update&quot;:
        $sSQL = &quot;update participants set &quot; .
          &quot;eventid=&quot; . tosql($fldeventid, &quot;Number&quot;) .
          &quot;,who=&quot; . tosql($fldwho, &quot;Number&quot;);
        $sSQL .= &quot; where &quot; . $sWhere;
    break;
    case &quot;delete&quot;:
        $sSQL = &quot;delete from participants where &quot; . $sWhere;
    break;
  }
// Execute SQL statement
  if(strlen($sparticipantErr)) return;
  if($bExecSQL)
  $db->query($sSQL);
  header(&quot;Location: &quot; . $sActionFileName . $sParams);
  exit;
}

The selection form name is &quot;who&quot; and that's the only thing that can be drawn from it.

I've tried chaning the case &quot;insert&quot; to create a loop (both a if and a while) but im coming up with empty inserts or just one record being inserted.

Any ideas on how to approach this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top