开发者

duplicate dropdown values on form post

开发者 https://www.devze.com 2023-01-26 15:26 出处:网络
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.

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"; 

    $resu开发者_运维知识库lt = 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.


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.


Maybe you are fetching duplicate values from the database? .. or looping twice which is of course easier to spot.


This is pretty far-fetched guess. Does it help if you add this after your while loop:

mysql_free_result($result);
0

精彩评论

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

关注公众号