Solved

insert multiple list box values into MySQL Database using PHP

Posted on 2013-05-22
9
3,186 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
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.

770 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