...

View Full Version : Select Drop Down Not Retaining DB Value



rock_14
05-29-2007, 04:58 PM
After a user enters information into a db I have an edit.php page where the user can update the information they enter.

While the edit.php page works correctly, it doesn't retain the value the user selected from select drop down / combo box type fields.

For example, I have a <select><option> type field for country and while it will pull up all the countries in tluCountries, it doesn't remember the value the user selected.

My code looks like this:



//open db
//execute sql



$countryName = array();
//last name into array
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());
while ($rowN = mysql_fetch_assoc($search_country_name))
{
array_push($countryName, $rowN["CountryName"]);
}

$mineral_country = $_POST['mineral_country'];

<select name="mineral_country"size="1" style="width: 50mm">
<?php foreach($countryName as $valueN) { ?>
<option value="<?= $valueN ?>" <? if ($_POST['CountryName'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
<?
}
?>


Does anyone see what is wrong? Thanks.

whizard
05-29-2007, 05:11 PM
Try printing out $_POST['CountryName'] to see what value it holds

HTH
Dan

rock_14
05-29-2007, 05:14 PM
Thanks for the help.

When I print the following I get:



echo 'country name = '.$mineral_country;

/* the output I get is:
country name = Array
*/

Brandoe85
05-29-2007, 05:17 PM
You named it mineral_country but are referencing Countryname. Change it to mineral_country and give it go.

Good luck.

rock_14
05-29-2007, 05:29 PM
You named it mineral_country but are referencing Countryname. Change it to mineral_country and give it go.

Good luck.


Thanks for the help. Here is the code now:



//open db and conn



$mineral_country = array();
//last name into array
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))
{
array_push($mineral_country, $rowN["mineral_country"]);
}

$mineral_country = $_POST['mineral_country'];

<select name="mineral_country"size="1" style="width: 50mm">
<?php foreach($countryName as $valueN) { ?>
<option value="<?= $valueN ?>" <? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
<?
}
?>
</select>Country Name<br>

/*
Here are the echo statements:
echo 'country name = '.$mineral_country.'<br>';
echo "$mineral_country = ".$_POST['mineral_country'];

And here is the output:

country name =
= Array

*/



Now, I get no countries shown when I click the <select><option> area for country. Any ideas?

whizard
05-29-2007, 05:41 PM
in your foreach loop, where is the variable '$countryName' getting it's value?

Dan

rock_14
05-29-2007, 05:46 PM
in your foreach loop, where is the variable '$countryName' getting it's value?

Dan

Thanks, I missed that when I was changing it over. It now reads:



<select name="mineral_country"size="1" style="width: 50mm">
<?php foreach($mineral_country as $valueN) { ?>
<option value="<?= $valueN ?>" <? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
<?
}
?>
</select>Country Name<br>

/*

echo 'country name = '.$mineral_country.'<br>';
echo "$mineral_country = ".$_POST['mineral_country'];

And the output is:

country name =
= Array

*/

whizard
05-29-2007, 05:51 PM
while ($rowN = mysql_fetch_assoc($search_country_name))
{
array_push($mineral_country, $rowN["mineral_country"]);
}

$mineral_country = $_POST['mineral_country'];

First, you make $mineral_country into an array and fill it with the values from the database. Then it would appear that you overwrite it with the $_POST value....

Dan

rock_14
05-29-2007, 06:00 PM
while ($rowN = mysql_fetch_assoc($search_country_name))
{
array_push($mineral_country, $rowN["mineral_country"]);
}

$mineral_country = $_POST['mineral_country'];

First, you make $mineral_country into an array and fill it with the values from the database. Then it would appear that you overwrite it with the $_POST value....

Dan

Thanks for the help. I have now rimmed out


//$mineral_country = $_POST['mineral_country']


And I still get no countries in the <select><option> area. Any ideas?

whizard
05-29-2007, 06:09 PM
echo 'country name = '.$mineral_country.'<br>';
echo "$mineral_country = ".$_POST['mineral_country'];

And the output is:

country name =
= Array

Is $_POST['mineral_country'] an array? because then your foreach loop wouldn't work

Dan

rock_14
05-29-2007, 06:28 PM
echo 'country name = '.$mineral_country.'<br>';
echo "$mineral_country = ".$_POST['mineral_country'];

And the output is:

country name =
= Array

Is $_POST['mineral_country'] an array? because then your foreach loop wouldn't work

Dan

Thanks for the help. Yes, $_POST['mineral_country'] is an array. I know I need to change something and also that I need an array somewhere to get the values and loop through them. Any idea on what I can do to get this to work?

rock_14
05-29-2007, 08:20 PM
Can anyone see my mistake now? What the script returns is a <select><option> area but with no values although the drop down box does open up and go down quite a ways like there are values but they aren't visible.


And the echo returns:

mineral country = Array



<?php
//open conn db

$mineral_country = array();
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))

{
array_push($mineral_country, $rowN["mineral_country"]);
}

?>

<select name="mineral_country"size="1" style="width: 50mm">

<?php foreach($mineral_country as $valueN) { ?>

<option value="<?= $valueN ?>" <? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

<?
}
?>

</select>Country Name<br>
<?php echo 'mineral country = '.$mineral_country;?>


Does anyone see what I am doing wrong?

whizard
05-29-2007, 09:04 PM
Hard for me to say, since I'm not fully sure what it is you're trying to do, but try this:


<?php
//open conn db

$mineral_country = array();
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))

{
array_push($mineral_country, $rowN["mineral_country"]);
}

?>

<select name="mineral_country"size="1" style="width: 50mm">

<?php foreach($mineral_country as $valueN) { ?>

<option value="<?= $valueN ?>" <? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

<?
}
?>

</select>Country Name<br>
<?php echo 'mineral country = '.$mineral_country;?>

HTH
Dan

rock_14
05-29-2007, 09:14 PM
Hard for me to say, since I'm not fully sure what it is you're trying to do, but try this:


<?php
//open conn db

$mineral_country = array();
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))

{
array_push($mineral_country, $rowN["mineral_country"]);
}

?>

<select name="mineral_country"size="1" style="width: 50mm">

<?php foreach($mineral_country as $valueN) { ?>

<option value="<?= $valueN ?>" <? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

<?
}
?>

</select>Country Name<br>
<?php echo 'mineral country = '.$mineral_country;?>

HTH
Dan

Dan,

Thank you for the help. This generates a different type of error when I try it. The error I get is wrong data type for second argument:




<option value="" <br />
<b>Warning</b>: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in <b>C:\xampp\htdocs\wbase\input_minerals\vaem.php</b> on line <b>40</b><br />
></option>


The structure of tluCountries is:

tluCountries:
CountryID (int 11)
CountryName (varchar 255)

Any ideas? And, thanks again for helping!

whizard
05-29-2007, 09:23 PM
That would mean that $_POST['mineral_country'] is not an array..

do this somewhere:

print_r($_POST['mineral_country']);

and see what it returns

I'm still trying to get the whole picture on what is happening here. Is it something like this?:

User loads page, after submitting a form. One of the form fields is $_POST['mineral_country']. (what type of field is this?)

Then, the page pulls out all the countries from a DB. The page compares each country to $_POST['mineral_country'] to see if that was the country the user selected in the last form.

(I guess I'm having trouble with $_POST['mineral_country'] here. Where does it come from? is it an array, or not? If it is an array, how are you trying to select a certain country in the select box based on its data?)

Dan

rock_14
05-29-2007, 09:52 PM
That would mean that $_POST['mineral_country'] is not an array..

do this somewhere:

print_r($_POST['mineral_country']);

and see what it returns

I'm still trying to get the whole picture on what is happening here. Is it something like this?:

User loads page, after submitting a form. One of the form fields is $_POST['mineral_country']. (what type of field is this?)

Then, the page pulls out all the countries from a DB. The page compares each country to $_POST['mineral_country'] to see if that was the country the user selected in the last form.

(I guess I'm having trouble with $_POST['mineral_country'] here. Where does it come from? is it an array, or not? If it is an array, how are you trying to select a certain country in the select box based on its data?)

Dan
Thanks,

Here is what it returns:



echo 'x='.print_r($_POST['mineral_country']);

/*
x=1
*/


This page is a simple update page where after a person fills in basic information and they are actually in the db they can then go to edit.php page to update the values. It all works fine except the <select><option> boxes.

whizard
05-29-2007, 10:03 PM
It looks like $_POST['mineral_country'] in not an array.

if $_POST['mineral_country'] holds the value '1', then I'm guessing it's referencing an ID in the DB, correct?

If so, you probably want your query to extract the country ID, not the country name...

HTH
Dan

rock_14
05-29-2007, 10:27 PM
It looks like $_POST['mineral_country'] in not an array.

if $_POST['mineral_country'] holds the value '1', then I'm guessing it's referencing an ID in the DB, correct?

If so, you probably want your query to extract the country ID, not the country name...

HTH
Dan

Dan,

Thanks, and yes, the value 1 does match the CountryID of the first country in the array. When I change the query to CountryID instead of CountryName the error is identical and does not change.

Basically, all I am trying to do is figure out how to:

1) Grab all the values from a lookup table from MySQL and put it into an array;

2) Loop through the array to find a match with what was entered in a different table;

3) Display the match as 'selected' in the combobox.

It shouldn't be so complicated, but I can't seem to get it to work.

Thanks for your patience.

iLLin
05-29-2007, 11:43 PM
Ok lets clean this up as your doing things multiple times. I am taking this as your working code.



<?php
//open conn db

$mineral_country = array();
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))

{
array_push($mineral_country, $rowN["mineral_country"]);
}

?>

<select name="mineral_country"size="1" style="width: 50mm">

<?php foreach($mineral_country as $valueN) { ?>

<option value="<?= $valueN ?>" <? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

<?
}
?>

</select>Country Name<br>
<?php echo 'mineral country = '.$mineral_country;?>


Now when you query your results from the database, they are put into an array already so there is not need to build another one. Like so:



<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["mineral_country"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["mineral_country"].'" '.$selected.'>'.$rowN["mineral_country"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>


See how that works for you.

rock_14
05-30-2007, 04:34 PM
Ok lets clean this up as your doing things multiple times. I am taking this as your working code.



<?php
//open conn db

$mineral_country = array();
$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name))

{
array_push($mineral_country, $rowN["mineral_country"]);
}

?>

<select name="mineral_country"size="1" style="width: 50mm">

<?php foreach($mineral_country as $valueN) { ?>

<option value="<?= $valueN ?>" <? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

<?
}
?>

</select>Country Name<br>
<?php echo 'mineral country = '.$mineral_country;?>


Now when you query your results from the database, they are put into an array already so there is not need to build another one. Like so:



<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["mineral_country"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["mineral_country"].'" '.$selected.'>'.$rowN["mineral_country"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>


See how that works for you.

Thank you for the help. I think this is getting much closer, but it still doesn't quite work. I tried the code below:



<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["mineral_country"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["mineral_country"].'" '.$selected.'>'.$rowN["mineral_country"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>

<?
//and echo for the following gave:

echo '$search_country_name = '.$search_country_name.'<br>';
echo 'mineral_country = '.$mineral_country;

/*
$search_country_name = Resource id #17
mineral_country = Array
*/

?>


Yet, I could see that it almost worked. Viewing the html for the page showed:


<select name="mineral_country"size="1" style="width: 50mm"><option value="" selected="selected"></option><option value="" selected="selected"></option><option value="" selected="selected"></option><option value="" selected="selected"></option><option value="" selected="selected"></option><option value="" selected="selected"></option><option value="" selected="selected"></option><option value=""


I could see that it did produce around 400 records to build this textbox, but did not populate it with countries. Any ideas?

Brandoe85
05-30-2007, 04:42 PM
Your query is this:


select CountryName from tluCountries

Then in your while loop you are using mineral_country


while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["mineral_country"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["mineral_country"].'" '.$selected.'>'.$rowN["mineral_country"].'</option>';
}

The column from your query should match with $rowN['something..']

Good luck.

rock_14
05-30-2007, 05:16 PM
Your query is this:


select CountryName from tluCountries

Then in your while loop you are using mineral_country


while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["mineral_country"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["mineral_country"].'" '.$selected.'>'.$rowN["mineral_country"].'</option>';
}

The column from your query should match with $rowN['something..']

Good luck.

Thank you for the help. I made the changes and the countries now appear. The only remaining issue is getting it to show the country that was previously selected.

I have a lookup type table, tluCountries, and the table where the country was originally stored, tblUser, when the user originally filled out the form. Somehow I need to compare the value in tblUser with every value in tluCountries to try and find a match.

Any ideas on how to do this? The change to the code I made is shown below:



<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["CountryName"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["CountryName"].'" '.$selected.'>'.$rowN["CountryName"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>

<?

iLLin
05-30-2007, 06:06 PM
<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["CountryName"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["CountryName"].'" '.$selected.'>'.$rowN["CountryName"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>


Ok this is only for when a user "POST's" the information in the form. Where is your code to query for the users stored information?

rock_14
05-30-2007, 06:16 PM
<?php
//open conn db

$query_country_name = "select CountryName from tluCountries";
$search_country_name = mysql_query($query_country_name)or die(mysql_error());

while ($rowN = mysql_fetch_assoc($search_country_name)) {
$selected = $_POST['mineral_country'] == $rowN["CountryName"] ? 'selected="selected"' : '';
$options .= '<option value="'.$rowN["CountryName"].'" '.$selected.'>'.$rowN["CountryName"].'</option>';
}
?>

<select name="mineral_country"size="1" style="width: 50mm"><?php echo $options; ?></select>Country Name<br>


Ok this is only for when a user "POST's" the information in the form. Where is your code to query for the users stored information?

I don't know how to do this part. I can write a SQL query and retrieve the country the user originally selected like this:



"SELECT CountryName from tblUser WHERE UserID = '$num'";


Where $num is a value I get from a query string when a user clicks on the page.

I suppose I could put this into a variable like:



$country_selected = "SELECT CountryName from tblUser WHERE UserID = '$num'";


And then compare it? I just am a bit unsure of what to do next. Any help will be greatly appreciated.

iLLin
05-30-2007, 06:39 PM
Ok so this is a edit page of a users profile?

You will need to select * there information anyway and when you do you will just replace the $_POST['mineral_country'] with the users value you got from the database.

Something to think about is if you have error checking and you re display your form when its submitted, they may change their country (doubtful) so you will need to setup a way for the new value to be displayed.

Something like this



$user_country = $user['country'];

if(posting information) {
$user_country = $_POST['the country'];
}


Then when you bump values it would be like this



$selected = $user_country == $rowN["CountryName"] ? 'selected="selected"' : '';

rock_14
05-31-2007, 04:43 PM
Ok so this is a edit page of a users profile?

You will need to select * there information anyway and when you do you will just replace the $_POST['mineral_country'] with the users value you got from the database.

Something to think about is if you have error checking and you re display your form when its submitted, they may change their country (doubtful) so you will need to setup a way for the new value to be displayed.

Something like this



$user_country = $user['country'];

if(posting information) {
$user_country = $_POST['the country'];
}


Then when you bump values it would be like this



$selected = $user_country == $rowN["CountryName"] ? 'selected="selected"' : '';


Thanks for the help. I have spent quite a while trying to get this and my code has become so mixed up, I can't seem to get it integrated.

If I have 2 tables, tblUser and tluCountry and they are constructed as follows:


tblUser
UserID
Country

tluCountry
CountryID
Country

And the user has registered and input a value for country and I want to build a drop down list for the edit page showing all the countries with his previous selection highlighted, how can I do this?

I know you already answered this, but I still am not getting it.

rock_14
05-31-2007, 08:18 PM
Thanks to everyone for the help. I got it resolved.:thumbsup:

CFMaBiSmAd
05-31-2007, 08:27 PM
For future reference, it is not necessary to quote entire posts when adding a new post. This makes for a physically long thread and makes it harder to find what is really being written. Only quote part of a post if you specifically need to call attention to something in that post.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum