Hi,

I have this form which has got all these drop down boxes which has values built in the html form, once selected they are all sent to the database table to be stored only when the authentication is done and for that record only.

I am also using the same form to retrieve information from the database and display back to the form. The problem is i am not able to display data retrieved from the database into the drop down boxes. Can someone please help me the code please.

Thanks much for the help.

<?php
include 'init.php';

if (!is_authed())
{
        header("Location: http://xxxx/wewewe/wee.ph");
        exit;
}
$ID = $_SESSION['ID'];

$results = mysql_query("SELECT * FROM `WorkExperience` WHERE ID=$ID");
$row = mysql_num_rows($results);
if($row == 0)
{
        mysql_query("INSERT INTO `WorkExperience` (ID) VALUES ('$ID')");
}

if($_POST['save'] || $_POST['next'])
{ 



$CountryOfEmployment = $_POST['CountryOfEmployment'];
$CommencementDate = $_POST['CommencementDate'];
$CommencementMonth = $_POST['CommencementMonth'];
$CommencementYear = $_POST['CommencementYear'];



$query = "UPDATE `WorkExperience` SET CountryOfEmployment='$CountryOfEmployment', CommencementDay='$CommencementDay', CommencementMonth='$CommencementMonth', CommencementYear='$CommencementYear' WHERE ID=$ID";


mysql_query($query);
}

if($_POST['next'])
{
        header("Location: gotonextpage.php");
        exit();
}

$query = "SELECT * FROM `WorkExperience` WHERE ID=$ID";
$results = mysql_query($query);
$data = mysql_fetch_array($results);


$CountryOfEmployment = $data['CountryOfEmployment'];
$CommencementDay = $data['CommencementDay'];
$CommencementMonth = $data['CommencementMonth'];
$CommencementYear = $data['CommencementYear'];


?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<TITLE>Graduate Admission System (Research Programme)</TITLE>

<link href="CSS/GraduateProgram.css" rel="stylesheet" type="text/css">

</head>

<body bgcolor="#FFFFFF" topmargin="0">
<br>
  <br>


<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="pg6.php"> 
  <TABLE  border=0 ALIGN="center" cellpadding = 3 cellspacing = 0  width="1000" style="border:1px solid #999999;">

<TR>
<td width="68%" align="left" class="BodyText">
				
				Country of Employment  
				
				
				 :	</td>
<td width=68% align="left" class="BodyText">
<select name="CountryOfEmployment" class="BodyText" style="color: 000000 ; font-weight: bold"
 value="<?=$CountryOfEmployment?>" id="CountryOfEmployment">
  <option> 
    <option
>AFGHANISTAN
      <option
>ALBANIA
        <option
>ALGERIA
        <option
>AMERICAN SAMOA
        <option
>ANDORRA
        <option
>ANGOLA
        <option
>ANTIGUA
        <option
>ARGENTINA
        <option
>ARMENIA 
        <option
>AUSTRALIA
        <option
>AUSTRIA
        <option
>AZERBAIJAN
        <option
>BAHAMAS
        <option
>BAHRAIN
        <option
>BANGLADESH
        <option
>BARBADOS
        <option
>BELARUS
        <option
>BELGIUM
        <option
>BELIZE
        <option
>BENIN
        <option
>BHUTAN
        <option
>BOLIVIA
        <option
>BOSNIA
        <option
>BOTSWANA
        <option
>BRAZIL
        <option
>BRITISH NATIONAL OVERSEAS
        <option
>BRUNEI
        <option
>BRUNEI PR
        <option
>BULGARIA
        <option
>BURKINA FASO
        <option
>BURUNDI
        <option
>CAMBODIA
        <option
>CAMEROON
        <option
>CANADA
        <option
>CAPE VERDE
        <option
>CENTRAL AFRICAN REP
        <option
>CHAD
        <option
>CHILE
        <option
>CHINA
        <option
>COLOMBIA
        <option
>COMOROS
        <option
>CONGO
        <option
>COOK ISLANDS
        <option
>COSTA RICA
        <option
>CROATIA
        <option
>CUBA
        <option
>CYPRUS
        <option
>CZECH REPUBLIC
        <option
>DENMARK
        <option
>DJIBOUTI
        <option
>DOMINICA
        <option
>DOMINICAN REP
        <option
>EAST TIMOR
        <option
>ECUADOR
        <option
>EGYPT
        <option
>EL SALVADOR
        <option
>EQUATORIAL GUINEA
        <option
>ERITREA
        <option
>ESTONIA
        <option
>ETHIOPIA
        <option
>FIJI
        <option
>FINLAND
        <option
>FRANCE
        <option
>FRENCH GUIANA
        <option
>FRENCH POLYNESIA
        <option
>GABON
        <option
>GAMBIA
        <option
>GEORGIA
        <option
>GERMANY
        <option
>GHANA
        <option
>GREECE
        <option
>GRENADA
        <option
>GUADELOUPE
        <option
>GUAM
        <option
>GUATEMALA
        <option
>GUINEA
        <option
>GUINEA-BISSAU
        <option
>GUYANA
        <option
>HAITI
        <option
>HONDURAS
        <option
>HONG KONG
        <option
>HUNGARY
        <option
>ICELAND
        <option
>INDIA
        <option
>INDONESIA
        <option
>IRAN
        <option
>IRAQ
        <option
>IRELAND
        <option
>ISRAEL
        <option
>ITALY
        <option
>IVORY COAST
        <option
>JAMAICA
        <option
>JAPAN
        <option
>JORDAN
        <option
>KAZAKHSTAN
        <option
>KENYA
        <option
>KIRIBATI
        <option
>KUWAIT
        <option
>KYRGYZSTAN
        <option
>LAOS
        <option
>LATVIA
        <option
>LEBANON
        <option
>LESOTHO
        <option
>LIBERIA
        <option
>LIBYA
        <option
>LIECHTENSTEIN
        <option
>LITHUANIA
        <option
>LUXEMBOURG
        <option
>MACAU
        <option
>MACEDONIA
        <option
>MADAGASCAR
        <option
>MALAWI
        <option
>MALAYSIA
        <option
>MALAYSIA PR
        <option
>MALDIVES
        <option
>MALI
        <option
>MALTA
        <option
>MARSHALL ISLANDS
        <option
>MARTINIQUE
        <option
>MAURITANIA
        <option
>MAURITIUS
        <option
>MEXICO
        <option
>MICRONESIA
        <option
>MONACO
        <option
>MONGOLIA
        <option
>MOROCCO
        <option
>MOZAMBIQUE
        <option
>MYANMAR
        <option
>NAMIBIA
        <option
>NAURU
        <option
>NEPAL
        <option
>NETHERLANDS
        <option
>NETHERLANDS ANTILLES
        <option
>NEW CALEDONIA
        <option
>NEW ZEALAND
        <option
>NICARAGUA
        <option
>NIGER
        <option
>NIGERIA
        <option
>NIUE ISLAND
        <option
>NORTH KOREA
        <option
>NORTHERN IRELAND
        <option
>NORWAY
        <option
>OMAN
        <option
>OTHERS
        <option
>PAKISTAN
        <option
>PALAU
        <option
>PALESTINE
        <option
>PANAMA
        <option
>PAPUA NEW GUINEA
        <option
>PARAGUAY
        <option
>PERU
        <option
>PHILIPPINES
        <option
>PITCAIRN ISLAND
        <option
>POLAND
        <option
>PORTUGAL
        <option
>PUERTO RICO
        <option
>QATAR
        <option
>REUNION
        <option
>ROMANIA
        <option
>RUSSIA
        <option
>RWANDA
        <option
>SAO TOME AND PRINCIPE
        <option
>SAUDI ARABIA
        <option
>SENEGAL
        <option
>SEYCHELLES
        <option
>SIERRA LEONE
        <option
>SINGAPORE
        <option
>SINGAPORE PR
        <option
>SLOVAK REPUBLIC
        <option
>SLOVENIA
        <option
>SOLOMON ISLANDS
        <option
>SOMALIA
        <option
>SOUTH AFRICA
        <option
>SOUTH KOREA
        <option
>SOUTH YEMEN
        <option
>SPAIN
        <option
>SRI LANKA
        <option
>ST. LUCIA
        <option
>ST. VINCENT
        <option
>STATELESS
        <option
>SUDAN
        <option
>SURINAME
        <option
>SWAZILAND
        <option
>SWEDEN
        <option
>SWITZERLAND
        <option
>SYRIA
        <option
>TAIWAN
        <option
>TAJIKISTAN
        <option
>TANZANIA
        <option
>THAILAND
        <option
>TOGO
        <option
>TOKELAU ISLANDS
        <option
>TONGA
        <option
>TRINIDAD AND TOBAGO
        <option
>TUNISIA
        <option
>TURKEY
        <option
>TUVALU
        <option
>UGANDA
        <option
>UKRAINE
        <option
>UNITED ARAB EMIRATES
        <option
>UNITED KINGDOM
        <option
>UNITED STATES
        <option
>URUGUAY
        <option
>UZBEKISTAN
        <option
>VANUATU
        <option
>VENEZUELA
        <option
>VIETNAM
        <option
>WALLIS AND FUTUNA
        <option
>WESTERN SAHARA
        <option
>YEMEN ARAB REPUBLIC
        <option
>YUGOSLAVIA
        <option
>ZAIRE
        <option
>ZAMBIA
        <option
>ZIMBABWE
      </select></td>
</TR>

<TR>
<td align="left" bgcolor=#E8E8E8 class="BodyText">
				
				Commencement Date(day/month/year)
				
				
				 :	</td>
<td width=68% align="left" bgcolor=#E8E8E8 class="BodyText">
    <select name="CommencementDate" class="BodyText" style="color: 000000 ; font-weight: bold"
 value="<?=$CommencementDate?>" id="CommencementDate">
      <option> 
        <option
>01
        <option
>02
        <option
>03
        <option
>04
        <option
>05
        <option
>06
        <option
>07
        <option
>08
        <option
>09
        <option
>10
        <option
>11
        <option
>12
        <option
>13
        <option
>14
        <option
>15
        <option
>16
        <option
>17
        <option
>18
        <option
>19
        <option
>20
        <option
>21
        <option
>22
        <option
>23
        <option
>24
        <option
>25
        <option
>26
        <option
>27
        <option
>28
        <option
>29
        <option
>30
        <option
>31
      </select>
/
<select name="CommencementMonth" class="BodyText" style="color: 000000 ; font-weight: bold"
 value="<?=$CommencementMonth?>" id="CommencementMonth">
  <option> 
    <option
>01
      <option
>02
        <option
>03
        <option
>04
        <option
>05
        <option
>06
        <option
>07
        <option
>08
        <option
>09
        <option
>10
        <option
>11
        <option
>12
      </select>

/
<select name="CommencementYear" class="BodyText" style="color: 000000 ; font-weight: bold"
 value="<?=$CommencementYear?>" id="CommencementYear">
  <option> 
    <option
>1968
      <option
>1969
        <option
>1970
        <option
>1971
        <option
>1972
        <option
>1973
        <option
>1974
        <option
>1975
        <option
>1976
        <option
>1977
        <option
>1978
        <option
>1979
        <option
>1980
        <option
>1981
        <option
>1982
        <option
>1983
        <option
>1984
        <option
>1985
        <option
>1986
        <option
>1987
        <option
>1988
        <option
>1989
        <option
>1990
        <option
>1991
        <option
>1992
        <option
>1993
        <option
>1994
        <option
>1995
        <option
>1996
        <option
>1997
        <option
>1998
        <option
>1999
        <option
>2000
        <option
>2001
        <option
>2002
        <option
>2003
        <option
>2004
        <option
>2005
        <option
>2006
        <option
>2007
        <option
>2008
      </select></td>
</TR>
</TABLE>
  <br />

<div id="div"></div>

  <div align="center">
    <p><br>
      <input name="next" type="submit" class="BodyText" id="Proceed" value="Proceed to Next Section">
      <input name="save" type="submit" class="BodyText"  value="Save Changes">
      <input name="reset" type="reset" class="BodyText" id="reset" value="Clear Form">
      <input name="logout" type="submit" class="BodyText"  value="Logout">
  </div>

</form>

  

</p>
    <p align="center">7/12<br>
    </p>


</BODY>
</HTML>

Recommended Answers

All 6 Replies

Use something like this

<?php
echo '<select name='dropdown' id='dropdown'>
<option> -- select -- </option>';

$sql = "SELECT * FROM table";
$result = mysql_query($sql);

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
}
echo '</select>';
?>

This should work for you. Obviously change the SQL to suit your database, etc.

Use something like this

<?php
echo '<select name='dropdown' id='dropdown'>
<option> -- select -- </option>';

$sql = "SELECT * FROM table";
$result = mysql_query($sql);

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
}
echo '</select>';
?>

This should work for you. Obviously change the SQL to suit your database, etc.

This works yes....but my question is i use the values in the drop down to insert into the database once i click the submit button, but the same fields which i use to insert have also have to display the information that has been entered into the database...as you see this just a part of the form that i am building.....i have a session id that will keep track of which user is logged and it will show only that particular users records...the text boxes are working but not the drop down boxes in this case...

do u mean while retrieving data u want the
drop down to be selected

do u mean while retrieving data u want the
drop down to be selected

Yes....with the data that corresponds to that particular user...

<?php
echo "<select name='dropdown' id='dropdown'>";


$sql = "SELECT country FROM tbl_user WHERE username='$username'";
$result = mysql_query($sql) or die("Error in sql: ".mysql_error());

$row=mysql_fetch_array($result);
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';

echo '</select>';
?>

That'll get the only value for them from the db

or

<select name="country">
<?php
$sqlx = "SELECT * FROM `tbl_user` ORDER BY `tbl_user`.`id`"; // assuming you have user IDs

$resultx = mysql_query($sqlx) or die("Error in part sqlX: ".mysql_error());

while($row = mysql_fetch_array($resultx)) {
        $user = $_SESSION['username']; // again assuming you're using session to stored logged in user, if it's GET then change accordingly.
           
 

if($user == $row['username']) {
$country = $row['country'];

                      
   echo '<option value='.$country.'>'.$country.'</option>'; ?>
</select>

That will also find all the values only for that user, assuming each user only has one country, it should just return one value.

Hope that helps.
Sam

@pancgom , why aren't you using array to store name of countries and then simple loop through array and insert them in drop down menu (foreach statement would be handy). Similar applies to date, month and year as they are numeric values, you can use for loop have look on these examples
If you do so, you will dramaticaly cut down number of lines in present code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.