close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Saving Multiple Selections

Thread began 11/25/2009 12:18 am by Jaffa | Last modified 11/24/2014 10:07 am by Jason Byrnes | 8527 views | 23 replies |

Jaffa

Saving Multiple Selections

I have a Dynamic Drop Down menu that allows the user to select multiple items, how do I get the update recordset behaviour to save the multiple choices in my MySQL database.

It only saves 1 choice rather than all of the selected ones.

Thanks

Sign in to reply to this post

Jason ByrnesWebAssist

with multiple select lists, the options are passed in an array, you can use the implode function to convert the array to a comma separated list:

php:
<?php echo(isset($_POST['elementName'])?implode(", "$_POST['elementName']):""); ?>
Sign in to reply to this post

elizabeth130596

Insert or Update select options into database (PHP/MySQL)

Hi - I have a table that has 6 fields. 4 fields have drop down menus. We want user to be able to select multiple options from within the drop downs. However, only 1 value is being inserted to record initially, and on update, only one option is being updated. We need to have multiple options from within the drop down menu/list added to the field on the database.

Up in the MySQL database, which I administer via PHP My Admin, I had set this field to datatype TEXT. Do I have to change that to ENUM or SET (which is giving me error # 1064?)

How do I resolve this? I am only adding or updating single record at a time. thanks.

Sign in to reply to this post

elizabeth130596

select options

Hi - Where does this line of code go on the page? before update? just after recordset definition? in the body of the page?

Sign in to reply to this post

elizabeth130596

proper placement of implode code for adding multiple options

Hi - Here is the code block that governs the drop down options list and inserting them into the database table. Question: where within this block does the IMPLODE stuff go?
--------------

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "update_practice")) {
$updateSQL = sprintf("UPDATE tblPractice1 SET memberID=%s, DIMENS=%s, INTERESTS=%s, languages=%s, ages_served=%s, treatment_techniques=%s, geo_location=%s WHERE practiceID=%s",
GetSQLValueString($_POST['memberID'], "int"),
GetSQLValueString($_POST['DIMENS'], "text"),
GetSQLValueString($_POST['INTERESTS'], "text"),
GetSQLValueString($_POST['languages'], "text"),
GetSQLValueString($_POST['ages_served'], "text"),
GetSQLValueString($_POST['treatment_techniques'], "text"),
GetSQLValueString($_POST['geo_location'], "text"),
GetSQLValueString($_POST['memberID'], "int"));

mysql_select_db($database_mem1, $mem1);
$Result1 = mysql_query($updateSQL, $mem1) or die(mysql_error());

$updateGoTo = "confirmation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

--------------

This is using straight DW update record behavior. Next post will include Data Assist Update Single Record Behavior. Neither DW nor WA-data assist can take the insert multiple options from drop down menus.
thanks,
elizabeth

Sign in to reply to this post

elizabeth130596

Drop Down multiple select options - hand-modify code for implode

Hi - Here is code block for same set of database table - update form, this time using the Data Assist Server Behaviors. Code block copied re. the update form feature.

so, in your previous post offering a workaround for allowing multiple options within a drop down list, how do we integrate this line:
<?php echo(isset($_POST['elementName'])?implode(", ", $_POST['elementName']):""); ?>

??

Thanks for your help.
-------------
<?php
// WA Application Builder Update
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $mem1;
$WA_table = "tblPractice1";
$WA_redirectURL = "confirmation.php";
$WA_keepQueryString = false;
$WA_indexField = "practiceID";
$WA_fieldNamesStr = "DIMENS|INTERESTS|languages|ages_served|treatment_techniques|geo_location";
$WA_fieldValuesStr = "".((isset($_POST["DIMENS"]))?$_POST["DIMENS"]:"") ."" . "|" . "".((isset($_POST["INTERESTS"]))?$_POST["INTERESTS"]:"") ."" . "|" . "".((isset($_POST["languages"]))?$_POST["languages"]:"") ."" . "|" . "".((isset($_POST["ages_served"]))?$_POST["ages_served"]:"") ."" . "|" . "".((isset($_POST["treatment_techniques"]))?$_POST["treatment_techniques"]:"") ."" . "|" . "".((isset($_POST["geo_location"]))?$_POST["geo_location"]:"") ."";
$WA_columnTypesStr = "',none,''|',none,''|',none,''|',none,''|',none,''|',none,''";
$WA_comparisonStr = "=|=|=|=|=|=";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);

$WA_where_fieldValuesStr = "".$row_rsPractice['practiceID'] ."";
$WA_where_columnTypesStr = "none,none,NULL";
$WA_where_comparisonStr = "=";
$WA_where_fieldNames = explode("|", $WA_indexField);
$WA_where_fieldValues = explode("|", $WA_where_fieldValuesStr);
$WA_where_columns = explode("|", $WA_where_columnTypesStr);
$WA_where_comparisons = explode("|", $WA_where_comparisonStr);

$WA_connectionDB = $database_mem1;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$updateParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WhereObj = WA_AB_generateWhereClause($WA_where_fieldNames, $WA_where_columns, $WA_where_fieldValues, $WA_where_comparisons );
$WA_Sql = "UPDATE `" . $WA_table . "` SET " . $updateParamsObj->WA_setValues . " WHERE " . $WhereObj->sqlWhereClause . "";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
------------

Sign in to reply to this post

Jason ByrnesWebAssist

The implode function goes in the code that performs the insert or update.

In the case of the dreamweaver insert record code, lets say the INTERESTS form element is a multiple select list, you would need to adjust the insert record code like this:


$updateSQL = sprintf("UPDATE tblPractice1 SET memberID=%s, DIMENS=%s, INTERESTS=%s, languages=%s, ages_served=%s, treatment_techniques=%s, geo_location=%s WHERE practiceID=%s",
GetSQLValueString($_POST['memberID'], "int"),
GetSQLValueString($_POST['DIMENS'], "text"),
GetSQLValueString(implode(", ", $_POST['INTERESTS']), "text"),
GetSQLValueString($_POST['languages'], "text"),
GetSQLValueString($_POST['ages_served'], "text"),
GetSQLValueString($_POST['treatment_techniques'], "text"),
GetSQLValueString($_POST['geo_location'], "text"),
GetSQLValueString($_POST['memberID'], "int"));



In the case of the DataAssist Update page:

$WA_fieldValuesStr = "".((isset($_POST["DIMENS"]))?$_POST["DIMENS"]:"") ."" . "|" . "".((isset($_POST["INTERESTS"]))?implode(", ", $_POST["INTERESTS"]):"") ."" . "|" . "".((isset($_POST["languages"]))?$_POST["languages"]:"") ."" . "|" . "".((isset($_POST["ages_served"]))?$_POST["ages_served"]:"") ."" . "|" . "".((isset($_POST["treatment_techniques"]))?$_POST["treatment_techniques"]:"") ."" . "|" . "".((isset($_POST["geo_location"]))?$_POST["geo_location"]:"") ."";
$WA_columnTypesStr = "',none,''|',none,''|',none,''|',none,''|',none,'' |',none,''";


The options you selected in the select list are being sent as an array. The implode function is converting the array to a coma separated list.


In the case of an Insert page that is fine, but when on an update page, you need to add custom code to preselect the items in the select list based on the comma separated list in the database.


The following thread has information on how to accomplish that:
showthread.php?t=4045&highlight=in_array

Sign in to reply to this post

elizabeth130596

Implode function causes header errors

Hi - I have altered the code to include the implode function at the SELECT menu, using it on the DW insert record behavior. I am now getting this error message:

-------------------

Warning: implode() [function.implode]: Invalid arguments passed in /home/content/77/5308377/html/maint/practice_add.php on line 41

Warning: Cannot modify header information - headers already sent by (output started at /home/content/77/5308377/html/maint/practice_add.php:41) in /home/content/77/5308377/html/maint/practice_add.php on line 55
--------------------

HERE is the PHP code from the header, minus the connection script:

----------------

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_practice")) {
$insertSQL = sprintf("INSERT INTO tblPractice1 (memberID, DIMENS, INTERESTS, languages, ages_served, treatment_techniques, geo_location) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['memberID'], "int"),
GetSQLValueString($_POST['DIMENS'], "text"),
GetSQLValueString(implode(", ", $_POST['INTERESTS']), "text"),
GetSQLValueString($_POST['languages'], "text"),
GetSQLValueString($_POST['ages_served'], "text"),
GetSQLValueString($_POST['treatment_techniques'], "text"),
GetSQLValueString($_POST['geo_location'], "text"));

mysql_select_db($database_mem1, $mem1);
$Result1 = mysql_query($insertSQL, $mem1) or die(mysql_error());

$insertGoTo = "confirmation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

$colname_rsPractice = "-1";
if (isset($_GET['memberID'])) {
$colname_rsPractice = (get_magic_quotes_gpc()) ? $_GET['memberID'] : addslashes($_GET['memberID']);
}
mysql_select_db($database_mem1, $mem1);
$query_rsPractice = sprintf("SELECT memberID, name_last, name_first FROM tblMembers WHERE memberID = %s", GetSQLValueString($colname_rsPractice, "int"));
$rsPractice = mysql_query($query_rsPractice, $mem1) or die(mysql_error());
$row_rsPractice = mysql_fetch_assoc($rsPractice);
$totalRows_rsPractice = mysql_num_rows($rsPractice);?>

---------------

HERE is the code for the altered field INTERESTS:

---------------

<select name="INTERESTS" size="5" multiple="multiple" id="INTERESTS">
<option value="Addictive Disorders - Alcoholism">Addictive Disorders
- Alcoholism</option>
<option value="Addictive Disorders - Drugs / Substance Abuse">Addictive
Disorders - Drugs / Substance Abuse</option>
<option value="Addictive Disorders - Gambling">Addictive Disorders
- Gambling</option>
</select>
--------------

got the same error message while using the WA behaviors.

Thanks for your help,
Elizabeth

Sign in to reply to this post

elizabeth130596

have line numbers to determine error message references

Hi -
header errors:
Line 41 was this: GetSQLValueString(implode(", ", $_POST['INTERESTS']), "text"),

Line 55 was this: header(sprintf("Location: %s", $insertGoTo));

Sign in to reply to this post

Jason ByrnesWebAssist

Please send a copy of your page so I can examine the code in context.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...