dongshijiao2363 2010-11-22 19:39
浏览 13

表格帖子上的重复下拉值

I have a form in which there is a dropdown list. this list is populated with values from the database. But, when I submit the form, the dropdown list gets duplicate values.

code -

<form name="form1" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">             

    <select name="test">
    <?php
        GetList();
    ?>
    </select>

    <input type="submit" value="submit" class="submit" />

</form>

my GetList function -

function GetList()
{   
     global $db;

    $sql = "SELECT id,text FROM table"; 

    $result = mysql_query ($sql);


    while($nt=mysql_fetch_array($result))
    {   //Array or records stored in $nt 
    echo '<option value='.$nt[id].'>'.$nt[text].'</option>'; 
    }

}

The dropdown list should be populated only once. I dont mind if the dropdown list is re-populated on form post but I dont want duplicate values in my dropdown list?

How do I get rid of the duplicate values?

edit - For e.g - if the dropdown displays values 1,2,3,4 when I first land on the page. After, i click submit, the dropdown list displays 1,2,3,4,1,2,3,4. And goes on that way as many times as I submit.

  • 写回答

3条回答 默认 最新

  • dongxiaoguang9108 2010-11-22 19:43
    关注

    Sounds like there's either duplicate data in your database, or your loop's logic makes it go twice somehow when you do a post. If you show the rest of your GetList code it will be easier to help you out.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图