Sandy Sailer
asked on
insert multiple list box values into MySQL Database using PHP
I can't figure out what is wrong with this code. It inserts all fields EXCEPT the which_state field. I've added the brackets [] after the select name, to make it an array, but it's not working. Thank you!
form field:
<select name="which_state[]" id="which_state[]" size="10" multiple="multiple" class="formMenufield_Mediu m" tabindex="5">
<option value="Colorado">Colorado< /option>
<option value="Florida">Florida</o ption>
<option value="Illinois">Illinois< /option>
<option value="Michigan">Michigan< /option>
<option value="Minnesota">Minnesot a</option>
<option value="Ohio">Ohio</option>
<option value="Texas">Texas</optio n>
<option value="Wisconsin">Wisconsi n</option>
</select>
insert script:
if ((isset($_POST["MM_insert" ])) && ($_POST["MM_insert"] == "SSContact_Sharp_Default") ) {
$insertSQL = sprintf("INSERT INTO emails (email, phone, full_name, which_state) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST[' Email_Addr ess'], "text"),
GetSQLValueString($_POST[' phone'], "text"),
GetSQLValueString($_POST[' Full_Name' ], "text"),
GetSQLValueString($_POST[' which_stat e[]'], "text"));
mysql_select_db($database_ allanb, $allanb);
$Result1 = mysql_query($insertSQL, $allanb) or die(mysql_error());
form field:
<select name="which_state[]" id="which_state[]" size="10" multiple="multiple" class="formMenufield_Mediu
<option value="Colorado">Colorado<
<option value="Florida">Florida</o
<option value="Illinois">Illinois<
<option value="Michigan">Michigan<
<option value="Minnesota">Minnesot
<option value="Ohio">Ohio</option>
<option value="Texas">Texas</optio
<option value="Wisconsin">Wisconsi
</select>
insert script:
if ((isset($_POST["MM_insert"
$insertSQL = sprintf("INSERT INTO emails (email, phone, full_name, which_state) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['
GetSQLValueString($_POST['
GetSQLValueString($_POST['
GetSQLValueString($_POST['
mysql_select_db($database_
$Result1 = mysql_query($insertSQL, $allanb) or die(mysql_error());
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can get the array to output doing the following:
if (isset($_POST['which_state '])) {
$ws = $_POST['which_state'];
foreach ($ws as $wch)
{
echo 'which states '.$wch;
}
}
Then, if I change the insert script to this,
if ((isset($_POST["MM_insert" ])) && ($_POST["MM_insert"] == "SSContact_Sharp_Default") ) {
$insertSQL = sprintf("INSERT INTO emails (email, phone, full_name, which_state) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST[' Email_Addr ess'], "text"),
GetSQLValueString($_POST[' phone'], "text"),
GetSQLValueString($_POST[' Full_Name' ], "text"),
GetSQLValueString($wch, "text"));
I would think that it would insert the array in the which_state field. It does not. Although, now it enters ONE of the states from the list, but not all of them.
I know I'm close, but I just can't figure it out.
if (isset($_POST['which_state
$ws = $_POST['which_state'];
foreach ($ws as $wch)
{
echo 'which states '.$wch;
}
}
Then, if I change the insert script to this,
if ((isset($_POST["MM_insert"
$insertSQL = sprintf("INSERT INTO emails (email, phone, full_name, which_state) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['
GetSQLValueString($_POST['
GetSQLValueString($_POST['
GetSQLValueString($wch, "text"));
I would think that it would insert the array in the which_state field. It does not. Although, now it enters ONE of the states from the list, but not all of them.
I know I'm close, but I just can't figure it out.
That's why I am showing you example of foreach loop above for values:
$which_state= $_POST['which_state'];
foreach($which_state as $ws){
echo $ws."<br/>";
}
change it to your method
ASKER
Finally - I got it to work!
GetSQLValueString(implode( ',', $_POST['which_state']), "text"));
GetSQLValueString(implode(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually, I got it working - see my post above. In the insert script, I used GetSQLValueString(implode. ..
ASKER
resolved it mostly myself.
ASKER
I changed my select statement to this: <select name="which_state[]" id="which_state" size="10" multiple="multiple" class="formMenufield_Mediu
Actually, the code above is only outputting the first item selected in the list.