Jump to content

Storing selection from dynamic listbox into database.


HarryAdney
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi everyone,

 

I'm a complete newbie here. I've looked around for help and tried a few solutions without success, so because time is short I'm asking for help.

 

I have a registration form which includes address details. The form holds a country field which is a listbox, the values being selected from a MySQL table. How do I assign the selected country option to a field list in the insert statement?

 

Here's what I have so far:

 

Registration page form:

 

<div class="form-group">
<input type="text" class="form-control" id="country" name="country" placeholder="Country">
 <select name="country_list">
  <?php 
           $sql = mysql_query("SELECT country_name FROM countries");
           while ($row = mysql_fetch_array($sql)){
           echo "<option value=\"country_list\">" . $row['country_name'] . "</option>";
           }
  ?>
</select>
</div>
 
 
 
The insert function page:
 
switch ($action) {
   case 'signup':
   switch ($action) {
   case 'signup':
   $country = clean($_POST['$selectedOption']);
etc.
 
 

The insert statement:

 

$sql_insert = "INSERT INTO user_mst(country)
VALUES('$country_name)";
$result_insert = mysql_query($sql_insert) or die(mysql_error());
$id = mysql_insert_id($conn);
 
In the code sections above, I've removed some of the other fields etc. to focus on the one bit I don't know howto fix.
 
Thanks in advance for any help.
Link to comment
Share on other sites

Hi thanks for getting back to me so quickly. I changed

 

$country = clean($_POST['$selectedOption']); to

$country = clean($_POST['$country_list]);

 

and tested, without success. Do I need to change the insert statement too? Something like:

 

$sql_insert = "INSERT INTO user_mst(country)

VALUES('$country_list)";
Link to comment
Share on other sites

 

 

$country = clean($_POST['$country_list]);

Why have you got a dollar sign before  country_list. There should not be a dollar sign there. 

 

Do you not know how to use associative arrays? or how to retrieve data from $_POST?

 

 

Do I need to change the insert statement too? Something like:

 

$sql_insert = "INSERT INTO user_mst(country)

VALUES('$country_list)";

 

$country_list should be $country

Link to comment
Share on other sites

Why have you got a dollar sign before  country_list. There should not be a dollar sign there. 

 

Do you not know how to use associative arrays? or how to retrieve data from $_POST?

 

 

$country_list should be $country

 

All of the other fields have the $ in front, i.e.

 

// Insert record
$sql_insert = "INSERT INTO user_mst(role, first_name, last_name, gender, dob, location, city, postcode, country, latitude, longitude, email, password, intel, email_activated, activation_token, d_added, d_updated)
VALUES('$role', '$first_name', '$last_name', '$gender', '$dob', '$location', '$city', '$postcode', '$country_list', '$latitude', '$longitude', '$email', '$password', '$intel', '$email_activated', '$activation_token', '$d_added', '$d_updated')";
$result_insert = mysql_query($sql_insert) or die(mysql_error());
$id = mysql_insert_id($conn);

 

I paid a guy on PeoplePerHour to do this code for me as I don't have time to do it myself. I'm learning in my own time but, as I say, I'm spending a lot of time on other projects. the developer who did the code for me appears to gone on holiday, unfortunately, so I'm stuck with trying to get it to work myself.

Link to comment
Share on other sites

  • Solution

 

 

All of the other fields have the $ in front, i.e.

No, I am not referring to that

 

To get the chosen item you need to use  $_POST['country_list']  there is no dollar sign before  country_list I repeat there is no dollar sign before  country_list

 

['country_list']  is not a variable, but a key to the $_POST superglobal (associative) array. This array is populated with the values submitted by your form in key/value pairs. The key being the name of an input field from your form. The value will be the value of that field.

 

So  $_POST['country_list']  will return the value of the chosen option from the  <select name="country_list"></select>  dropdown menu.

 

If you do not understand what arrays are then consult the manual here: http://php.net/arrays

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.