James
James

Reputation: 41

Insert Multiple Selection from Dropdown into one Row

I am trying to take multiple selections from a drop down, and insert them into a single column/row in a table.

So that it would look like this (ColumnB):

ColumnA | ColumnB | ColumnC
---------------------------
Stuff   | A, B, C | Stuff

This is the dropdown:

echo "<select NAME='SysName_1' SIZE='1' multiple tabindex='7'>\n";
echo "<option>Choose One or More</option>\n";

To populate dropdown I use a select statement

$strSQL = "SELECT STATEMENT"; 

$rsSQL  = odbc_exec($connSQL,$strSQL) or die ('Error Executing Subsystems SQL');

$strOptions = "";

while ($row=odbc_fetch_array($rsSQL)){

   $id          = trim($row["SysName"]);
   $thing       = trim($row["SysDesc"]);
   $strOptions .= "<OPTION VALUE='$id'>$thing</option>";
}

echo $strOptions;

echo "</select>\n";

And this is what I have at my Insert:

Should combine multiple selections separated by , correct?

$SysName_1 = implode(',',$_POST['$SysName_1']); 

$SQLIN = 'INSERT INTO TABLE (COLUMNA) VALUES ($SysName_1)';

I tried doing what was located here, as his code seems to be doing what I want (although he doesn't want it to) Multiple dropdown values inserting in a single row not in multiple row

But it does not work at all. I get an invalid argument for the implode in my server log, and the insert statement is just blank for that variable.

Any help would be appreciated.

Upvotes: 0

Views: 2527

Answers (1)

kero
kero

Reputation: 10638

The name attribute in your form needs to end with [] to indicate an array. This should work

echo "<select NAME='SysName_1[]' SIZE='1' multiple tabindex='7'>\n";

But you should be careful. Never trust user input! Don't insert the value from your $_POST directly into the db. Always escape at least

Upvotes: 1

Related Questions