0

Possible Duplicate:
How to make <option selected=“selected”> set by MySql and PHP?

I have made a select in my page. This Select draw the options from a database. The Select is:

<select name="name" id="name">
<?php
$pelatisquery="SELECT onoma_pelati FROM pelates_onoma";
$pelatisresult=mysql_query($pelatisquery) 
or die ("Query to get data from firsttable failed: ".mysql_error());
while ($pelatisorow=mysql_fetch_array($pelatisresult)) {
$onoma_pelati=$pelatisorow[onoma_pelati];
echo "<option>
$onoma_pelati
</option>";
}
?>
</select>

This select is in my main page. So in the Edit page I want to retrieve this Select with one option Selected from the database. So my Edit page is:

include_once "../../db_connection.php";
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT id,pelatis FROM poliseis WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$pelatis = $row['pelatis'];
}
}

So if the Select had options I will use this code:

<?php if($pelatis== "James") { echo 'selected = "selected"'; } ?>

But now how to make <option selected="selected"> ? Thank you in advance for your answers

Community
  • 1
  • 1

1 Answers1

1

Google is your friend.

<option value="" selected="selected"></option>

<option value="" selected></option> //works too but less cross-browser friendly
bobthyasian
  • 945
  • 5
  • 17