[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

insert multiple list box values into MySQL Database using PHP

Posted on 2013-05-22
9
Medium Priority
?
3,384 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this article, we’ll look at how to deploy ProxySQL.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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