So I have this drop down list in my form which pull “tags” from database as value for drop down options:
<select name="cartags">
<?php $result = mysql_query("SELECT * FROM Products WHERE ID > '0'");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['Tag']; echo "\""; echo ">"; echo $row['Tag']; echo "</option>";
}
?>
</select>
What is my problem? My problem is that I am adding a lot of products into my databas and my code make dropdown list with tags for all this producst even if they have same tag. So what I need is solution how to prevent that same tag appear twice in my drop down.
I am pretty new to PHP and this is my first question here so I really hope that I explained my problem well.
Thanks in advance!
What is the purpose of
WHERE ID > '0'? IfIDis an auto-increment then it will always be positive. If not, it should be.Why are you using
mysql_fetch_arrayand then only using the associative keys? You should usemysql_fetch_associnstead.Why are you using a new
echoevery time you want to output a variable? Just concatenate.Why are you setting the same string in
valueas the option’s text? Without a value, it defaults to the text anyway.Why are you not using backticks around your column and table names?
Try this instead: