HarryAdney Posted July 20, 2014 Share Posted July 20, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/ Share on other sites More sharing options...
Ch0cu3r Posted July 20, 2014 Share Posted July 20, 2014 You can retrieve the selected option from the country dropdown list using $_POST['country_list'] Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485754 Share on other sites More sharing options...
HarryAdney Posted July 20, 2014 Author Share Posted July 20, 2014 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)"; Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485755 Share on other sites More sharing options...
Ch0cu3r Posted July 20, 2014 Share Posted July 20, 2014 $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 Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485756 Share on other sites More sharing options...
HarryAdney Posted July 20, 2014 Author Share Posted July 20, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485757 Share on other sites More sharing options...
Solution Ch0cu3r Posted July 20, 2014 Solution Share Posted July 20, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485759 Share on other sites More sharing options...
HarryAdney Posted July 20, 2014 Author Share Posted July 20, 2014 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/290019-storing-selection-from-dynamic-listbox-into-database/#findComment-1485760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.