Solved

insert multiple list box values into MySQL Database using PHP

Posted on 2013-05-22
9
3,151 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
  • 5
  • 3
9 Comments
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 333 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ssailer
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Jagadishwor Dulal
Comment Utility
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
Comment Utility
Finally - I got it to work!  

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

Accepted Solution

by:
Dave Baldwin earned 167 total points
Comment Utility
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
Comment Utility
Actually, I got it working - see my post above.  In the insert script, I used GetSQLValueString(implode...
0
 

Author Closing Comment

by:ssailer
Comment Utility
resolved it mostly myself.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now