0

hello .. m trying to get/fetch the database value into dropdown box as a selected value in the dropdown box using PHP script and mysql - phpmyadmin database ..

but m having problems in it .. here is my script which i m using ..

try.php

<body>

<?php

$conn = mysql_connect("localhost","root","") or die("could not connect to localhost");
mysql_select_db("test") or die("could not connect to database");
$qry = "select * from table1 where id=1";
$result = mysql_query($qry) or die(mysql_error());
?>
<select name="date">
<?php 
while($rows = mysql_fetch_array($result))
{
$myDate=$rows['date'];
?>
Date : 

<?php

for ($date=1;$date<=31;$date++)
    {
        if ($myDate==$date)
        {
            $s="selected='selected'";
        }
        else
        {
            $s="";
        }
?>
    <option value="<?php echo $date; ?>" <?php echo $s ;?>><?php echo $date; ?></option>



<?php 
}
?>


<?php
}
?>

</select>
</body>

m getting the values from 1 TO 31 in my drodown box

BUT NOT ABLE TO GET THE "SELECTED" VALUE WHICH IS 25 IN MY DATABASE

..please somebody tell me .. where is wrong m here in this script . .

Edited by pyTony: fixed formatting

3
Contributors
2
Replies
12
Views
6 Years
Discussion Span
Last Post by ddymacek
0

Sorry I m not getting your question? what you are matching ? you want in dropdown box only the the dates to be display which are in database?

0

I downloaded and altered so I could run your code. it appears to be fine. are you getting an error? pull up your page source after you execute the code and see use firebug to inspect your elements and you can see which one is 'selected'. I defaulted my $myDate to 25, because that is said what your db was returning. also you could echo out what number your db is returning (as a debug step) to make sure it is '25' or whatever expected date you are pulling out. perhaps you are not getting the right data in the first place. the code 'works' as far as I can tell.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.