insert multiple choices into mysql field
Copy link to clipboard
Copied
GetSQLValueString($_POST['not_reen_list'], "text"),
and the HTML:
<select name="not_reen_list" size="10" multiple="multiple" id="not_reen_list">
<option value="Nothing selected" selected="selected">Please select one</option><option value="Cost">Cost</option>
<option value="School could not meet our financial aid needs"> School could not meet our financial aid needs </option>
<option value="Prefer another school">Prefer another school </option>
</select>
Any help is appreciated.
Copy link to clipboard
Copied
> I have a form that inserts a record into mysql. it's working fine except when i
> changed the pull-down menu to a list allowing multiple selections, it only
> inserts the last item selected.
That's because you're not sending the form data as an array.
Change this:
<select name="not_reen_list"
to this:
<select name="not_reen_list[]"
At the top of the PHP script, convert the array to a comma-separated
string by adding this:
if (isset($_POST['not_reen_list'])) {
$_POST['not_reen_list'] = implode(',', $_POST['not_reen_list']);
}
I know it should insert the valuse as comma
> separated. Here's the php:
> GetSQLValueString($_POST['not_reen_list'], "text"),
> and the HTML:
> <select name="not_reen_list" size="10" multiple="multiple" id="not_reen_list">
> <option value="Nothing selected" selected="selected">Please select
> one</option><option value="Cost">Cost</option>
> <option value="School could not meet our financial aid needs"> School
> could not meet our financial aid needs </option>
> <option value="Prefer another school">Prefer another school </option>
> </select>
> Any help is appreciated.
>
--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

Copy link to clipboard
Copied
Now the question I have is there a way to then return that data back in to the multiple select box and have it selected?
Many thanks.
Copy link to clipboard
Copied
> This helped me as well.
Great.
> Now the question I have is there a way to then return that data back in to the multiple select box and have it selected?
When you retrieve the results from the database, the list will be a
comma-separated string. You need to turn it into an array, and test
whether the item is in the array.
// Get the list of options from the recordset
$options = $row_recordsetName['list_options'];
// Convert them to an array
$opts = explode(',', $options);
// Initialize an empty array
$trimmed = array();
// Loop through the array of options to remove any whitespace
foreach ($opts as $item) {
$trimmed[] = trim($item);
}
Inside the multiple-choice list, check whether the option is in the
$trimmed array:
<option value="football" <?php if (in_array('football', $trimmed)) {
echo 'selected="selected"'; } ?>>Football</option>
<option value="tennis" <?php if (in_array('tennis', $trimmed)) {
echo 'selected="selected"'; } ?>>Tennis</option>
--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
i have just used dacid powers code, it places all the content in one feild, is this correct?
Copy link to clipboard
Copied
>i have just used dacid powers code, it places
>all the content in one feild, is this correct?
Storing more than one value in a database column violates rules of database normalization. Generally, it should be avoided. What is the nature of the data you are storing?
Copy link to clipboard
Copied
it has stored size ID's into one feild in the DB, i didnt think it was correct so didnt use it, i wanted to use the multiple select and store each individual ID in different feilds
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO beauStock (StockID, ID, SizeID, Stock, Sold) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['StockID'], "int"),
GetSQLValueString($_POST['ID'], "text"),
GetSQLValueString($_POST['SizeID'], "text"),
GetSQLValueString($_POST['Stock'], "text"),
GetSQLValueString($_POST['sold'], "text"));
<select name="SizeID[]" multiple="multiple">
Copy link to clipboard
Copied
i need to store the sizes as individual records in the database because this is how they need to be retrieved (as individual records so when i use the mutiple select list rather then insert them as comma seperated sizes thee need to be individually stored

