Jump to content

selecting the database value in a list


vinaip

Recommended Posts

Hi,

 

I am doing a PHP website using Dreamveaver built in server behaviors, and am getting my hands wet on PHP now.

 

I have a list of countries in a drop down menu list in a form. A user selects a country, and it is saved in the database.

 

now on the Edit Screen, I want to show the list of countries again to the user for any changes, but I want to pre-select the value that was selected originally, and was stored in the database.

 

so this time, the dropdown menu box should show the saved country by default, which could be changed.

 

how do I do this in Dreamveaver. If it is not straightforward, please suggest the PHP code to do this, and I will try to update it myself in the code window.

 

Thanks a lot.

 

Vinai

Link to comment
https://forums.phpfreaks.com/topic/43642-selecting-the-database-value-in-a-list/
Share on other sites

Hello Vinai,

 

I work in Dreamweaver predominantly, but I only ever use it in the 'code' view. Therefore, I can only give you the code you need. Try the following:

 

<form action="somewhere.php" method="post">
Country list:
<select name="country_select">
<?php
// Change the following to match your settings:
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname ='the database name';

// Connect to your database:
$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
  {
  die('Could not connect to the database: ' . mysql_error());
  }
// Select the database
mysql_select_db($dbname, $con);

// Get the field from the database:
$result = mysql_query("SELECT country FROM $dbname LIMIT 1");

while($row = mysql_fetch_array($result))
{
$country = $row['country'];
// Use the data as the pre-selected drop down option:
echo '<option value="'.$country.'">'.$country.'</option>';
}

mysql_close($con);
?>
<!-- Put the complete country list here: -->
<option value="Algeria">Algeria</option>
<option value="Albania">Albania</option>
<!-- etc... -->
</select>

 

I hope that helps you!

Regards,

Iceman

Thanks Iceman for your quick response. I have done this much.. that is, I am showing a list of all countries in the dropdown. Let's say the user selects "Singapore" as the country. I then save this in the database as the Shipping Country.

 

We provide an entry editing screen, where this list of Countries is shown again, but this time, we want the value that is shown to the user is preset to "Singapore". But the user is free to choose any other country. So the drop down must contain all the countries, but the chosen/selected one should be preset / prefilled to show Singapore.

 

How do I achieve this in PHP?

 

Any help is appreciated.

 

Thanks,

Vinai

 

Make the following change to the above code:

 

// Get the field from the database:
$result = mysql_query("SELECT country FROM $dbname LIMIT 1");
$userInfo = mysql_query("SELECT country FROM $userDB WHERE userID = '$userID'");
$userCountry = mysql_result($userInfo, 0, 'country');

while($row = mysql_fetch_array($result))
{
     $country = $row['country'];
     // Use the data as the pre-selected drop down option:
     echo '<option value="'.$country.'"';
     if($country == $userCountry)
          echo " selected=\"selected\"";
     echo '>'.$country.'</option>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.