-1

我试图在下拉列表中打印选定的值,但徒劳无功。我是 php 和 html 的新手,所以这听起来像是一个愚蠢的问题,但请帮帮我!这是我的代码:

echo '<tr><td>Client:</td><td><select name="client_name">';   
            $sql = mysql_query("SELECT * FROM client");
            $s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");
          //  $s2= mysql_fetch_array($s);
            while ($row = mysql_fetch_array($sql))
            {
               while ($s2==mysql_fetch_array($s))
               { 
                if ($row['client_id'] == $s2['client_id'])
                 $selected = "selected=\"selected\"";
                   else
                        $selected = " ";                                                                         
               }     
               echo '<option value="' . $row['client_id'] . '" ' . ($selected == $row['client_id'] ? ' selected' : '') . '>' . $s2['client_name'] . '</option>';
            }

此代码不起作用。请帮帮我!有不同的方法吗?

4

2 回答 2

1

尝试这个

echo '<tr><td>Client:</td><td><select name="client_name">';   
        $sql = mysql_query("SELECT * FROM client");
        $s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");
      //  $s2= mysql_fetch_array($s);
        while ($row = mysql_fetch_array($sql))
        {
           while ($s2==mysql_fetch_array($s))
           { 
            if ($row['client_id'] == $s2['client_id'])
             $selected = "selected=\"selected\"";
               else
                    $selected = " ";                                                                         
           }     
           echo '<option value="' . $row['client_id'] . '" ' . $selected . '>' . $s2['client_name'] . '</option>';
        }`
于 2013-04-29T05:17:47.787 回答
0

这个查询有问题

$s= mysql_query("SELECT project.client_id, client_name, client.client_id FROM client,project where project.client_id=client.client_id AND project_id='$editId'");

您正在尝试获取两个客户端 ID project.client_id 和 client.client_id,在这种情况下,您应该将 project.client_id 等别名作为 project_client_id 并将 client.client_id 作为 client_client_id,然后使用该值在 while 循环中进行比较。

请注意使用您想要的任何客户端 ID,无论是 project_client_id 还是 client_client_id。

于 2013-04-29T05:18:28.437 回答