如何创建包含用户所有特定行的选择?

时间:2012-05-26 15:59:20

标签: php mysql database select

我尝试制作一个应该发布的选择并包含来自用户的所有投票

  <select name="votacao" size="1">

     <option>

       <?php
         $sql = "SELECT nome_votacao FROM votacoes WHERE user_id = $_SESSION['id']";
         $rs = mysql_query($sql);                       
         while($row = mysql_fetch_row($rs)) 
             echo $row['nome_votacao'];             
       ?>   

    </option>

</select>

3 个答案:

答案 0 :(得分:1)

您的<option>需要在您的循环中:

<select name="votacao" size="1">


       <?php
         $sql = "SELECT nome_votacao FROM votacoes WHERE user_id = $_SESSION['id']";
         $rs = mysql_query($sql);                       
         while($row = mysql_fetch_row($rs)) {
             echo sprintf("<option>%s</option>\n", $row['nome_votacao']);  
         }      
       ?>   



</select>

您可能还需要为每个value添加一个唯一ID作为<option>属性,以确保真正有用。

答案 1 :(得分:0)

<select name="votacao" size="1"> 
       <?php 
         $sql = "SELECT nome_votacao FROM votacoes WHERE user_id = $_SESSION['id']"; 
         $rs = mysql_query($sql);                        
         while($row = mysql_fetch_row($rs))  
             echo "<option>".$row['nome_votacao']."</option>";              
       ?>
</select>

答案 2 :(得分:0)

试试这个

<select name="votacao" size="1">

     <option>

       <?php
         $sql = "SELECT nome_votacao FROM votacoes WHERE user_id = $_SESSION['id']";
         $rs = mysql_query($sql);     
          foreach ($rs as $row)        
          { ?>

           <option> <?php echo $row['nome_votacao']; ?> </option>
       <? } ?>   

    </option>