?
Solved

insert multiple list box values into MySQL Database using PHP

Posted on 2013-05-22
9
Medium Priority
?
3,331 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 999 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 999 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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 84

Accepted Solution

by:
Dave Baldwin earned 501 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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