Forums

This topic is locked

list/menu Insert Record form question?

Posted 11 Feb 2005 01:10:46
1
has voted
11 Feb 2005 01:10:46 Bobby Edgar posted:
Is it possible to have a list/menu giving people the option to select several things, and put those selected options into one database field? I need to allow my users to list the types of jobs they wish to have mailed to them. They need to be able to select more than one option, and then the script will take the information passed by a new job posting, search that field for users who have choosen to get jobs such at the one just posted, and mail it to them. Make Sense?

I recently switched to PHP/MYSQL and this used to work fine when I used ASP/Access.

Here is my code:

<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<font color="#666666" size="3">
<select name="Type" size="6" multiple>
<option selected>Choose One</option>
<option>Administrative</option>
<option>Designer</option>
<option>Technician</option>
<option>Director</option>
<option>Choreographers</option>
<option>Management</option>
<option>Musician</option>
<option>Internship</option>
<option>Office Staff</option>
<option>Writers Wanted</option>
<option>Volunteers</option>
<option>Educational</option>
<option>Large/Multiple Calls</option>
<option>Other / Misc.</option>
</select>
<input type="submit" name="Submit" value="Submit">
</font>
<input type="hidden" name="MM_insert" value="form1">
</form>

When I do this, it only places the last choosen one in the field. It must be possible, and I have scoured the web for two days looking for the answer.

Bobby
DMX2004 / XP-Sp2 / PHP/MYSQL<font color=blue></font id=blue>

Replies

Replied 11 Feb 2005 01:25:47
11 Feb 2005 01:25:47 Vince Baker replied:
problem is that you actually need to store these items in an array and then turn them into a comma seperated list.....

I will write you a bit of code tomorrow first thing.

Regards

Vince Baker
<strong>DMX Zone Manager</strong>

[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
Replied 11 Feb 2005 01:34:07
11 Feb 2005 01:34:07 Bobby Edgar replied:
Thank you so much. I appreciate the quick response. THANKS DMXZONE!!!

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
problem is that you actually need to store these items in an array and then turn them into a comma seperated list.....

I will write you a bit of code tomorrow first thing.

Regards

Vince Baker
&lt;strong&gt;DMX Zone Manager&lt;/strong&gt;

[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Replied 11 Feb 2005 02:12:10
11 Feb 2005 02:12:10 Chris Charlton replied:
<b>MySQL datatype SET</b> would be the choice to save comma-seperated values in one column (field), and as for HTML output a multiselect list/menu is prime for that.

Hope that helps. <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

<b>Further detail:</b> you can send MySQL the string value or even a #, because internally MySQL stores an array for the SET values. <b>Example:</b> SET ('apples','oranges','plums')... you can INSERT/UPDATE and send 'apple,plums' or 0,2 as MySQL registers them the same. I would read more on MySQLs' site as they explain better - www.mysql.com .

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>

Edited by - ccharlton on 11 Feb 2005 02:16:16
Replied 11 Feb 2005 03:04:10
11 Feb 2005 03:04:10 Bobby Edgar replied:
I have done as you said:

MySql Field Setup
Type= set('Administrative','Designer','Technician','Director','Choreographer','Management','Musician','Internship','Office Staff','Writers Wanted','Volunteers','Educational','Large/Multiple Calls','Other / Misc.')

Html Form Code:
&lt;select name="Type" size="6" multiple class="forms" id="Type"&gt;
&lt;option&gt;Administrative&lt;/option&gt;
&lt;option&gt;Designer&lt;/option&gt;
&lt;option&gt;Technician&lt;/option&gt;
&lt;option&gt;Director&lt;/option&gt;
&lt;option&gt;Choreographers&lt;/option&gt;
&lt;option&gt;Management&lt;/option&gt;
&lt;option&gt;Musician&lt;/option&gt;
&lt;option&gt;Internship&lt;/option&gt;
&lt;option&gt;Office Staff&lt;/option&gt;
&lt;option&gt;Writers Wanted&lt;/option&gt;
&lt;option&gt;Volunteers&lt;/option&gt;
&lt;option&gt;Educational&lt;/option&gt;
&lt;option&gt;Large/Multiple Calls&lt;/option&gt;
&lt;option&gt;Other / Misc.&lt;/option&gt;
&lt;/select&gt;

It still only posts the last choice selected from the list/menu. I referenced the mysql site as you suggested, and believe I am following instructions.

I really apprecaite the help, as this has been driving me crazy for days now..

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
<b>MySQL datatype SET</b> would be the choice to save comma-seperated values in one column (field), and as for HTML output a multiselect list/menu is prime for that.

Hope that helps. <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

<b>Further detail:</b> you can send MySQL the string value or even a #, because internally MySQL stores an array for the SET values. <b>Example:</b> SET ('apples','oranges','plums')... you can INSERT/UPDATE and send 'apple,plums' or 0,2 as MySQL registers them the same. I would read more on MySQLs' site as they explain better - www.mysql.com .

~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>

Edited by - ccharlton on 11 Feb 2005 02:16:16
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Replied 11 Feb 2005 04:53:57
11 Feb 2005 04:53:57 Bobby Edgar replied:
I have been researching some more, and yes it appears that it must be put into an array. I have done that much, but have no clue how to then out that array into the database. Your help will save my brain!!! You can view my page at this URL: theatreresourcegroup.org/staffing_main_jobs_direct_register_checkboxes.php

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
problem is that you actually need to store these items in an array and then turn them into a comma seperated list.....

I will write you a bit of code tomorrow first thing.

Regards

Vince Baker
&lt;strong&gt;DMX Zone Manager&lt;/strong&gt;

[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Replied 11 Feb 2005 17:58:31
11 Feb 2005 17:58:31 Bobby Edgar replied:
Here is the code generated when the form is submitted. It creates the array from the list/menu, but I have no idea how to place the array results into the table...

Array
(
[FirstName] =&gt; Robert
[LastName] =&gt; Edgar
[EmailAddress] =&gt; ????
[user_ip] =&gt; ????
[direct_id] =&gt; 389265493
[UserID] =&gt; ????
[JobType] =&gt; Array
(
[0] =&gt; Administrative
[1] =&gt; Technician
[2] =&gt; Choreographers
[3] =&gt; Musician
[4] =&gt; Internship
[5] =&gt; Writers Wanted
[6] =&gt; Educational
)

[Submit2] =&gt; Sign Me Up
[MM_insert] =&gt; jobs_direct
)

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
problem is that you actually need to store these items in an array and then turn them into a comma seperated list.....

I will write you a bit of code tomorrow first thing.

Regards

Vince Baker
&lt;strong&gt;DMX Zone Manager&lt;/strong&gt;

[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Replied 12 Feb 2005 19:05:35
12 Feb 2005 19:05:35 Bobby Edgar replied:
Can anyone help me with this? I didn't get the code as I was told I would. I am really stuck with my site creation until I can get this figured out...
Replied 11 Aug 2006 11:33:44
11 Aug 2006 11:33:44 Julian Hockings replied:
This is a very late reply, but may be helpful to someone in the future. Basically, you can't really use DW's built-in behaviors to take a multiple select list and perform an insert record. You can, but only the last selection in the list will be inserted. In order to get all the multiple selections inserted into the database, you have to do some hand-coding.

First, you need to rename your form's select tag from name="Type" to name="Type[]". Putting those brackets in at the end of the name will make it so that when PHP receives the form contents it will turn them into an array. Then you have to take DW's usual insert record code and put it within a loop that loops through each of the array items. Here's an example:

// set it up
reset ($Type); // this sets the array pointer to the beginning of the array
mysql_select_db($database, $db);

// loop through array of Type[] and write each instance to db
while (list($key, $val) = each ($Type)) {
$insertSQL = sprintf("INSERT INTO yourtable (yourfield) VALUES (%s)",
GetSQLValueString($val, "text");
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
} // end loop

Note that you also need to have the GetSQLValueString function on the page somewhere. DW automatically inserts this function whenever you do an insert record behavior. You can copy and paste it from another page if you like. Also be sure to have the include file to your connection settings somewhere on the page.
Replied 04 Sep 2006 08:23:54
04 Sep 2006 08:23:54 Brian Mr. replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
This is a very late reply, but may be helpful to someone in the future. Basically, you can't really use DW's built-in behaviors to take a multiple select list and perform an insert record. You can, but only the last selection in the list will be inserted. In order to get all the multiple selections inserted into the database, you have to do some hand-coding.

First, you need to rename your form's select tag from name="Type" to name="Type[]". Putting those brackets in at the end of the name will make it so that when PHP receives the form contents it will turn them into an array. Then you have to take DW's usual insert record code and put it within a loop that loops through each of the array items. Here's an example:

// set it up
reset ($Type); // this sets the array pointer to the beginning of the array
mysql_select_db($database, $db);

// loop through array of Type[] and write each instance to db
while (list($key, $val) = each ($Type)) {
$insertSQL = sprintf("INSERT INTO yourtable (yourfield) VALUES (%s)",
GetSQLValueString($val, "text");
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
} // end loop

Note that you also need to have the GetSQLValueString function on the page somewhere. DW automatically inserts this function whenever you do an insert record behavior. You can copy and paste it from another page if you like. Also be sure to have the include file to your connection settings somewhere on the page.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

What value would I place in the "$key" ?
thx
Replied 26 Sep 2006 18:58:51
26 Sep 2006 18:58:51 Afonso Marques replied:
Thanks! Thanks! Thanks! Thanks!

I was starting to melt down with this issue.
After tons of articles, websites, forum searches and few thousands of other things, finally the answer!
It works, and my problem is solved!

PS: Why doesn't Adobe simplify this stuff on the next update or version of DW? Just a thought...
Replied 30 Dec 2006 08:36:37
30 Dec 2006 08:36:37 Tim Lehmann replied:
Hello,

i have the same Problem,
where must i insert the code in my Site,
here is the Code of my Recordset:

Thanks for Helping.
Best Regards
Tim

&lt;?php require_once('Connections/local.php'); ?&gt;
&lt;?php
// Load the common classes
require_once('includes/common/KT_common.php');

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("";

// Make unified connection variable
$conn_local = new KT_connection($local, $database_local);

// Start trigger
$formValidation = new tNG_FormValidation();
$tNGs-&gt;prepareValidation($formValidation);
// End trigger

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;
}
}

mysql_select_db($database_local, $local);
$query_languages = "SELECT * FROM sbdtng_languages ORDER BY sbdtng_languages.sb_desc";
$languages = mysql_query($query_languages, $local) or die(mysql_error());
$row_languages = mysql_fetch_assoc($languages);
$totalRows_languages = mysql_num_rows($languages);

mysql_select_db($database_local, $local);
$query_currencies = "SELECT * FROM sbdtng_currencies_all ORDER BY sbdtng_currencies_all.sb_desc";
$currencies = mysql_query($query_currencies, $local) or die(mysql_error());
$row_currencies = mysql_fetch_assoc($currencies);
$totalRows_currencies = mysql_num_rows($currencies);

// Make an insert transaction instance
$ins_users = new tNG_insert($conn_local);
$tNGs-&gt;addTransaction($ins_users);
// Register triggers
$ins_users-&gt;registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1";
$ins_users-&gt;registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_users-&gt;registerTrigger("END", "Trigger_Default_Redirect", 99, "test.php";
// Add columns
$ins_users-&gt;setTable("users";
$ins_users-&gt;addColumn("first_name", "STRING_TYPE", "POST", "first_name";
$ins_users-&gt;addColumn("last_name", "STRING_TYPE", "POST", "last_name";
$ins_users-&gt;addColumn("email_address", "STRING_TYPE", "POST", "email_address";
$ins_users-&gt;addColumn("username", "STRING_TYPE", "POST", "username";
$ins_users-&gt;addColumn("languages", "STRING_TYPE", "POST", "languages";
$ins_users-&gt;addColumn("currencies", "STRING_TYPE", "POST", "currencies";
$ins_users-&gt;addColumn("password", "STRING_TYPE", "POST", "password";
$ins_users-&gt;addColumn("signup_date", "DATE_TYPE", "POST", "signup_date";
$ins_users-&gt;setPrimaryKey("userid", "NUMERIC_TYPE";

// Execute all the registered transactions
$tNGs-&gt;executeTransactions();

// Get the transaction recordset
$rsusers = $tNGs-&gt;getRecordset("users";
$row_rsusers = mysql_fetch_assoc($rsusers);
$totalRows_rsusers = mysql_num_rows($rsusers);
?&gt;&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;test&lt;/title&gt;
&lt;link href="includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" /&gt;
&lt;script src="includes/common/js/base.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="includes/common/js/utility.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="includes/skins/style.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;?php echo $tNGs-&gt;displayValidationRules();?&gt;
&lt;style type="text/css"&gt;
&lt;!--
.style2 {color: #999999}
--&gt;
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
echo $tNGs-&gt;getErrorMsg();
?&gt;
&lt;form method="post" id="form1" action="&lt;?php echo KT_escapeAttribute(KT_getFullUri()); ?&gt;"&gt;
&lt;table cellpadding="2" cellspacing="0" class="KT_tngtable"&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="first_name"&gt;First_name:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="first_name" id="first_name" value="&lt;?php echo KT_escapeAttribute($row_rsusers['first_name']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("first_name";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "first_name"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="last_name"&gt;Last_name:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="last_name" id="last_name" value="&lt;?php echo KT_escapeAttribute($row_rsusers['last_name']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("last_name";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "last_name"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="email_address"&gt;Email_address:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="email_address" id="email_address" value="&lt;?php echo KT_escapeAttribute($row_rsusers['email_address']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("email_address";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "email_address"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="username"&gt;Username:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="username" id="username" value="&lt;?php echo KT_escapeAttribute($row_rsusers['username']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("username";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "username"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="languages"&gt;Languages:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;select name="languages" size="3" multiple id="languages"&gt;
&lt;?php
do {
?&gt;
&lt;option value="&lt;?php echo $row_languages['sb_desc']?&gt;"&lt;?php if (!(strcmp($row_languages['sb_desc'], $row_rsusers['languages']))) {echo "SELECTED";} ?&gt;&gt;&lt;?php echo $row_languages['sb_desc']?&gt;&lt;/option&gt;
&lt;?php
} while ($row_languages = mysql_fetch_assoc($languages));
$rows = mysql_num_rows($languages);
if($rows &gt; 0) {
mysql_data_seek($languages, 0);
$row_languages = mysql_fetch_assoc($languages);
}
?&gt;
&lt;/select&gt;
&lt;?php echo $tNGs-&gt;displayFieldError("users", "languages"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="currencies"&gt;Currencies:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;select name="currencies" size="3" multiple id="currencies"&gt;
&lt;?php
do {
?&gt;
&lt;option value="&lt;?php echo $row_currencies['sb_desc']?&gt;"&lt;?php if (!(strcmp($row_currencies['sb_desc'], $row_rsusers['currencies']))) {echo "SELECTED";} ?&gt;&gt;&lt;?php echo $row_currencies['sb_desc']?&gt;&lt;/option&gt;
&lt;?php
} while ($row_currencies = mysql_fetch_assoc($currencies));
$rows = mysql_num_rows($currencies);
if($rows &gt; 0) {
mysql_data_seek($currencies, 0);
$row_currencies = mysql_fetch_assoc($currencies);
}
?&gt;
&lt;/select&gt;
&lt;?php echo $tNGs-&gt;displayFieldError("users", "currencies"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="password"&gt;Password:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="password" id="password" value="&lt;?php echo KT_escapeAttribute($row_rsusers['password']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("password";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "password"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="KT_th"&gt;&lt;span class="style2"&gt;
&lt;label for="signup_date"&gt;Signup_date:&lt;/label&gt;
&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="signup_date" id="signup_date" value="&lt;?php echo KT_formatDate($row_rsusers['signup_date']); ?&gt;" size="32" /&gt;
&lt;?php echo $tNGs-&gt;displayFieldHint("signup_date";?&gt; &lt;?php echo $tNGs-&gt;displayFieldError("users", "signup_date"; ?&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr class="KT_buttons"&gt;
&lt;td colspan="2"&gt;&lt;input name="KT_Insert1" type="submit" id="KT_Insert1" value="Insert record" /&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;p&gt;&nbsp;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;&lt;?php
mysql_free_result($languages);

mysql_free_result($currencies);
?&gt;

First, you need to rename your form's select tag from name="Type" to name="Type[]". Putting those brackets in at the end
of the name will make it so that when PHP receives the form contents it will turn them into an array. Then you have to take
DW's usual insert record code and put it within a loop that loops through each of the array items. Here's an example:

// set it up
reset ($languages); // this sets the array pointer to the beginning of the array
mysql_select_db($database, $db);

// loop through array of languages[] and write each instance to db
while (list($key, $val) = each ($languages)) {
$insertSQL = sprintf("INSERT INTO users (languages) VALUES (%s)",
GetSQLValueString($val, "text");
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
} // end loop

Note that you also need to have the GetSQLValueString function on the page somewhere. DW automatically inserts this
function whenever you do an insert record behavior. You can copy and paste it from another page if you like.
Also be sure to have the include file to your connection settings somewhere on the page.
Replied 26 Jul 2007 22:52:50
26 Jul 2007 22:52:50 Charity Babcock replied:
(I just emailed the following to Julian, but if anyone else has a solution, I would LOVE to hear!) Thanks!

Hi Julian!

About a year ago, you posted on a FWZone Forum a solution for uploading arrays to a database. I've found the information incredibly helpful, but am running into a small problem and was wondering if you had any ideas as to what I can do to fix it. I would be eternally grateful.

You posted the following:
Posted - 11 Aug 2006 : 11:33:44
This is a very late reply, but may be helpful to someone in the future. Basically, you can't really use DW's built-in behaviors to take a multiple select list and perform an insert record. You can, but only the last selection in the list will be inserted. In order to get all the multiple selections inserted into the database, you have to do some hand-coding.

First, you need to rename your form's select tag from name="Type" to name="Type[]". Putting those brackets in at the end of the name will make it so that when PHP receives the form contents it will turn them into an array. Then you have to take DW's usual insert record code and put it within a loop that loops through each of the array items. Here's an example:

// set it up
reset ($Type); // this sets the array pointer to the beginning of the array
mysql_select_db($database, $db);

// loop through array of Type[] and write each instance to db
while (list($key, $val) = each ($Type)) {
$insertSQL = sprintf("INSERT INTO yourtable (yourfield) VALUES (%s)",
GetSQLValueString($val, "text");
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
} // end loop

Note that you also need to have the GetSQLValueString function on the page somewhere. DW automatically inserts this function whenever you do an insert record behavior. You can copy and paste it from another page if you like. Also be sure to have the include file to your connection settings somewhere on the page.

______________

I've done this on my page, and it looks like this:

reset($specialty);
mysql_select_db($database_connection, $connection);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "memberinfo");
while (list($key, $val) = each ($specialty)) {
$insertSQL = sprintf("INSERT INTO specialty_group (specialty, member_id) VALUES (%s,LAST_INSERT_ID())",
GetSQLValueString($val, "text",
GetSQLValueString($_REQUEST['member_id'], "int");

mysql_select_db($database_connection, $connection);
$Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());

_____________________

My problem is this: the code performs perfectly, creating a new row in the table for each item selected in the list, along with their member_id. BUT, I get an error at the top of the page that reads

"Warning: reset() [function.reset]: Passed variable is not an array or object in /home/.alcuin/wiltern/aialosangeles.org/database/memberinfo.php on line 87

Warning: Variable passed to each() is not an array or object in /home/.alcuin/wiltern/aialosangeles.org/database/memberinfo.php on line 90"

I've tried to create an array using $specialty = array("adaptive reuse","art" and etc...
But it screws up how the data is entered and gives me the value "Array" in the database.

I love the result of your code, but don't know what to do to get rid of the pesky error message!

Any ideas you have would be most appreciated.

Thanks!

Reply to this topic