Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

insert multiple choices into mysql field

Explorer ,
Jun 02, 2008 Jun 02, 2008
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. 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.
TOPICS
Server side applications
3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2008 Jun 02, 2008
awsweb wrote:
> 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 07, 2008 Jun 07, 2008
This helped me as well.

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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 08, 2008 Jun 08, 2008
123abc456xyz wrote:
> 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2008 Jun 02, 2008
Thank you David. I appreciate your help.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 09, 2008 Jun 09, 2008
omg XD so complecated codes XD, thanks anyway
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2008 Jun 17, 2008
yes they do look terribly complicated
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 22, 2012 Sep 22, 2012

i have just used dacid powers code, it places all the content in one feild, is this correct?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 24, 2012 Sep 24, 2012

>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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2012 Sep 24, 2012

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">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 01, 2012 Oct 01, 2012
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines