/    Sign up×
Community /Pin to ProfileBookmark

2 select lists, one doesn’t write to db

I have 2 select lists I’m trying to write to a database. The first one in the form works, but the second one doesn’t. What am I doing wrong?

The select portion:

[code=php]
echo “<select name=’skillselect1′ size=1><option value=”>–Select–</option><option value=’1′>Barrel Racing</option><option value=’2′>Beginner/Family</option><option value=’3′>Breeding</option><option value=’4′>Brood Mare</option><option value=’5′>Calf Roping</option><option value=’6′>Cutting</option><option value=’7′>Draft</option><option value=’8′>Dressage</option><option value=’9′>Driving</option><option value=’10’>Endurance Riding</option><option value=’11’>English Pleasure</option><option value=’12’>Equitation</option><option value=’13’>Eventing</option><option value=’14’>Field Hunter</option><option value=’15’>Gaited</option><option value=’16’>Halter</option><option value=’17’>Harness</option><option value=’18’>Hunter</option><option value=’19’>Jumper</option><option value=’20’>Lesson Horse</option><option value=’21’>Longe-Line</option><option value=’22’>Pleasure Driving</option><option value=’23’>Pole Bending</option><option value=’24’>Polo</option><option value=’25’>Racing</option><option value=’26>Ranch Horse</option><option value=’27’>Reined Cow Horse</option><option value=’28’>Reining</option><option value=’29’>Steer Wrestling</option><option value=’30’>Team Penning</option><option value=’31’>Team Roping</option><option value=’32’>Team Sorting</option><option value=’33’>Trail Horse</option><option value=’34’>Vaulting</option><option value=’35’>Western Pleasure</option><option value=’36’>Western Pleasure (Show)</option><option value=’37’>Working Cow Horse</option><option value=’38’>Youth/4-H Horse</option></select>”;

echo “<select name=’skillselect2′ size=1><option value=”>–Select–</option><option value=’1′>Barrel Racing</option><option value=’2′>Beginner/Family</option><option value=’3′>Breeding</option><option value=’4′>Brood Mare</option><option value=’5′>Calf Roping</option><option value=’6′>Cutting</option><option value=’7′>Draft</option><option value=’8′>Dressage</option><option value=’9′>Driving</option><option value=’10’>Endurance Riding</option><option value=’11’>English Pleasure</option><option value=’12’>Equitation</option><option value=’13’>Eventing</option><option value=’14’>Field Hunter</option><option value=’15’>Gaited</option><option value=’16’>Halter</option><option value=’17’>Harness</option><option value=’18’>Hunter</option><option value=’19’>Jumper</option><option value=’20’>Lesson Horse</option><option value=’21’>Longe-Line</option><option value=’22’>Pleasure Driving</option><option value=’23’>Pole Bending</option><option value=’24’>Polo</option><option value=’25’>Racing</option><option value=’26>Ranch Horse</option><option value=’27’>Reined Cow Horse</option><option value=’28’>Reining</option><option value=’29’>Steer Wrestling</option><option value=’30’>Team Penning</option><option value=’31’>Team Roping</option><option value=’32’>Team Sorting</option><option value=’33’>Trail Horse</option><option value=’34’>Vaulting</option><option value=’35’>Western Pleasure</option><option value=’36’>Western Pleasure (Show)</option><option value=’37’>Working Cow Horse</option><option value=’38’>Youth/4-H Horse</option></select>”;[/code]

and the preview portion:

[code=php] if ($skillselect1 == ‘1’) {
$caskill1 = “Barrel Racing”;
}elseif ($skillselect1 == ‘2’) {
$caskill1 = “Beginner/Family”;
}elseif ($skillselect1 == ‘3’) {
$caskill1 = “Breeding”;
}elseif ($skillselect1 == ’23’) {
$caskill1 = “Pole Bending”;
}else {
$caskill1 = “”;
}

if ($skillselect2 == ‘1’) {
$caskill2 = “Barrel Racing”;
}elseif ($skillselect1 == ‘2’) {
$caskill2 = “Beginner/Family”;
}elseif ($skillselect1 == ‘3’) {
$caskill2 = “Breeding”;
}elseif ($skillselect1 == ’23’) {
$caskill2 = “Pole Bending”;
}else {
$caskill2 = “”;
}
[/code]

(the preview portion has been truncated in order to preserve space, but basically there are enough elseif’s to cover everything from the select lists values.) I don’t understand, tho, why it would write to the first field but not the second. The database field names are caskill1 and caskill2. When the form containing both these select lists is submitted, the first skill is written but the second one isn’t.

Thanks!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 26.2005 — Most of the following probably has nothing to do with your problem, but might be worth doing in order to clean things up and make debugging easier.

Do you actually do anything with the numeric values from your select options? If not, just delete the value='n' from the option tags. Then your posted value for that element will be set to the text value between the <option> and </option> tags, saving you all that if/else stuff (which could probably be better done with switch/case, by the way).

The code snippets you provided suggest that you are running with register_globals on. In order to make your code work if it's not on, and thus be more portable (plus I'd advise running with it off if you have control of that), reference the form values as $_POST array elements, e.g. $_POST['skillselect2'] instead of just $skillselect2. So, if we did both of these things, those long if/else things would simply become (with some defensive coding added to help us debug):
[code=php]
if(!empty($_POST['skillselect1']))
{
$caskill1 = $_POST['skillselect1'];
}
else
{
die("skillselect1 not posted from form");
}
if(!empty($_POST['skillselect2']))
{
$caskill2 = $_POST['skillselect2'];
}
else
{
die("skillselect2 not posted from form");
}
[/code]

Assuming we get through this section without dying, we'll need to see the code used to do the database update. I'd recommend building your query string, then for debugging right now echo out before doing the actual mysql_query() so you can verify it looks the way you intended it (plus add some debugging info if mysql returns an error):
[code=php]
$query = "INSERT INTO table_name SET caskill1 = '$caskill1', caskill2 = '$caskill2'";
echo "<p>DEBUG: query = '$query'</p>n"; ### DEBUG - delete before fielding ###
mysql_query($query) or die("SQL ERROR: $query - " . mysql_error());
[/code]
Copy linkTweet thisAlerts:
@lilqhgalauthorNov 27.2005 — Thanks NogDog, I got it working finally!! I do have another question now, though. What if I don't want the select lists to be required, in other words, if one of them is just ignored, then nothing gets passed to the db. (PS, I absolutely just am in LOVE with your pooch!) ?


[code=php]
if(!empty($_POST['skillselect1']))
{
$caskill1 = $_POST['skillselect1'];
}
else
{
die("skillselect1 not posted from form");
}
if(!empty($_POST['skillselect2']))
{
$caskill2 = $_POST['skillselect2'];
}
else
{
die("skillselect2 not posted from form");
}
[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@NogDogNov 27.2005 — You could make the first option for each select an empty string, using value= in this case so we can have some text indicating to the user that they are selecting nothing:
[code=html]
<select name="skillselect1">
<option value="">[none]</option>
<option>Option 1</option>
<option>Option 2</option>
<!-- etc. -->
</select>
[/code]

Then, if you're not worried about empty values, just assign it without the empty() checks above:
[code=php]
$caskill1 = $_POST['skillselect1'];
[/code]

When you do your insert, it will simply insert an empty string for that field if "[none]" was selected.
Copy linkTweet thisAlerts:
@lilqhgalauthorNov 29.2005 — okay I'm running into one more problem. I have it writing to the database fine for all select lists (I actually have 5, just used 2 for the example). But the problem I'm having is when I open the edit file, those lists aren't populated again, and when the file is saved, since those aren't populated, they become blank. How can I have the selected drop-down list populated with the corresponding database field (that the user selected in the write stage)?
Copy linkTweet thisAlerts:
@chazzyNov 29.2005 — what is this edit file?
Copy linkTweet thisAlerts:
@NogDogNov 29.2005 — You have to query the DB before outputting the form. For each select option, check to see if it is the value from the DB. If so, add the "selected" attribute to that option tag (or "selected='selected'" if using XHTML). It's usually easiest to do this by putting the list of options into a PHP loop:
[code=php]
# get user info:
$query = "SELECT * FROM table_name WHERE user_name = '$user'";
$result = mysql_query($query) or die(mysql_error());
$userData = mysql_fetch_assoc($result);
# ... start form, then for select ...
echo "<select name='select1'>n";
# output "none" selection:
$selected = ($userData['select1'] == "") ? " selected" : "";
echo "<option value=''$selected>[none]</option>n";
# loop through each possible option value:
$options = array('option1', 'option2', 'option3');
foreach($options as $value)
{
$selected = ($userData['select1'] == $value) ? " selected" : "";
echo "<option$selected>$value</option>n";
}
echo "</select>n";
[/code]
×

Success!

Help @lilqhgal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...