Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

Update dropdown list using PHP and MySQL

What I am trying to do is full some information from a mysql database and this will populate some fields and a drop down list. What i'm having some issues with is how to I change the drop down to what the column in my table is set to? Blow is the code of my list that pulls all the values, how do I set it to the value of the current record selected? Thanks
<td>
					
                <?php
$con = mysql_connect("localhost","root","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db('ats');
 
$query = mysql_query("SELECT names FROM department ORDER BY names ASC") or die (mysql_error());
?>
<select name="department">
<?php
while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
  $department = htmlentities($data['names']);
  echo '<option value="'.$department.'">'.$department."</option>\n";
}
?>
</select>           
                </td>

Open in new window

0
maximus81
Asked:
maximus81
  • 2
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
I don't quite understand.  What you have is done before it is sent to the browser.  In the browser window, the user gets to select one of the options.  What do you mean "current record selected?"
0
 
maximus81Author Commented:
I want to be able to pull a record from my mysql database and have that the current selection in my drop down. The user will then have the ability to change whats selected and update the record. The thing I don't know how to do is when I get the record from the database how to I have that the selection that's in the record?
0
 
ukerandiCommented:
try this
<td>
					
                <?php
$con = mysql_connect("localhost","root","Pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db('ats');
 
$query = mysql_query("SELECT names FROM department ORDER BY names ASC") or die (mysql_error());
?>
<Form method="get" action="<?php echo  $_SERVER['PHP_SELF'];?>">
<select name="department">
<?php
while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
  $department = htmlentities($data['names']);
  echo '<option value="'.$department.'">'.$department."</option>\n";
}
?>
</select>  
<input type="Submit" value="select">
</form>

         
<?php

$department=$_GET['department'];


$sql="select * from department where names='$department'";
$query1 = mysql_query($sql);
while ($data1 = mysql_fetch_array($query, MYSQL_ASSOC))
{
  
echo $data1['enterfieldnames1'];
echo $data1['enterfieldnames2'];
}

?>

                </td>

Open in new window

0
 
ukerandiCommented:
while ($data1 = mysql_fetch_array($query1, MYSQL_ASSOC))
0
 
maximus81Author Commented:
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now