Solved

insert multiple list box values into MySQL Database using PHP

Posted on 2013-05-22
9
3,236 Views
Last Modified: 2013-10-17
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_Medium" tabindex="5">
                            <option value="Colorado">Colorado</option>
                            <option value="Florida">Florida</option>
                            <option value="Illinois">Illinois</option>
                            <option value="Michigan">Michigan</option>
                            <option value="Minnesota">Minnesota</option>
                            <option value="Ohio">Ohio</option>
                            <option value="Texas">Texas</option>
                            <option value="Wisconsin">Wisconsin</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_Address'], "text"),
                       GetSQLValueString($_POST['phone'], "text"),
                       GetSQLValueString($_POST['Full_Name'], "text"),
                       GetSQLValueString($_POST['which_state[]'], "text"));

  mysql_select_db($database_allanb, $allanb);
  $Result1 = mysql_query($insertSQL, $allanb) or die(mysql_error());
0
Comment
Question by:ssailer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 333 total points
ID: 39188246
Why are you using [] in $_POST value remove it like:
GetSQLValueString($_POST['which_state'], "text"));

Open in new window

0
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 333 total points
ID: 39188262
Again you are using array in id also it is not necessary. Your select should be:
<select name="which_state[]" id="which_state" size="10" multiple="multiple" class="formMenufield_Medium" tabindex="5">

Open in new window

when you post your form try checking the value first like:

	$which_state= $_POST['which_state'];
	foreach($which_state as $ws){
	 echo $ws."<br/>";
	}

Open in new window

0
 

Author Comment

by:ssailer
ID: 39188419
I can get the selections to output as an array ... it just won't write the values to the database.  

I changed my select statement to this:  <select name="which_state[]" id="which_state" size="10" multiple="multiple" class="formMenufield_Medium" tabindex="5">

Actually, the code above is only outputting the first item selected in the list.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ssailer
ID: 39188477
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_Address'], "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.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39188496
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/>";
	}

Open in new window

change it to your method
0
 

Author Comment

by:ssailer
ID: 39188519
Finally - I got it to work!  

GetSQLValueString(implode(',', $_POST['which_state']), "text"));
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 167 total points
ID: 39188538
You have to extract the values from the array and put them in a string that can be inserted into the field.  The array name is just a pointer to the data structure.  And I believe that $wch in the foreach is strictly a local variable for the foreach.  If you create a variable outside the foreach and just concatenate $wch to it ($states .= $wch.' ';), then you would have data you could use in the INSERT.
0
 

Author Comment

by:ssailer
ID: 39188552
Actually, I got it working - see my post above.  In the insert script, I used GetSQLValueString(implode...
0
 

Author Closing Comment

by:ssailer
ID: 39580379
resolved it mostly myself.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question