开发者

Grabbing the right record in a drop down from mysql

I'm using a form where the user can edit an entry. Everything is populating and all is well with the exception that I can't get the drop down to show the project that they've already assigned the image to.

$project_qry = "SELECT * from projects ORDER BY title ASC";
$project_res = mysql_query($project_qry);

$project_drop = "<select name=\"project_id\">\n";
while ($row = mysql_fetch_array($project_res))
    {
    if ($project_id == $row[title])
        {
            $project_drop .= "<option value=\"$row[id]\" selected>$row[title]</option>\n";
        }
    else
        {
            $project_drop .= "<option value=\"$row[id]\">$row[title]</option开发者_运维百科>\n";
        }
}
$project_drop .= "</select>\n";

I'm sure it's something devilishly simple but I'm stumped.


{
    if ($project_id == $row[id])
        {
            $project_drop .= "<option value=\"$row[id]\" selected=\"selected\">$row[title]</option>\n";
        }
    else
        {
            $project_drop .= "<option value=\"$row[id]\">$row[title]</option>\n";
        }
}

You need to compare the value and not the title. It is the value that gets posted ($_POST)

selected="selected" makes it XHTML compliant.


bigstylee answered correctly. I also recommend to separate the values of the array and your string:

$project_drop .= "<option value='". $row['id'] ."'>".$row['title']."</option>";

Also drop \n. Outputting \n won't generate a line break in the browser. And it is unnecessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜