Getting 3 duplicate records in a drop down

breeze351
breeze351 used Ask the Experts™
on
I'm getting 3 records in a drop down even tho there is only one record.

		echo '<select id="company" name = "Broker" width="200" style="width: 200px">';
		$SqlString = "select * from comp order by COMPANY";
		$companies = $conn->query($SqlString);
		while ($row_comp = $companies->fetch_array())
		{
			echo "<option value = \"";
			echo $row_comp['TID'];
			echo "\"";
			if ($row_comp['TID'] == $row_survey_data['TID'])
		 	{
				echo " selected";
			}
			echo ">";						
			echo $row_comp['COMPANY'];
			echo "</option>";
		}
		echo "</select>";
		echo "<br>";

Open in new window


What bothers me is that if it was a loop why only 3 times.  I have the same problem with the "persons" that work for the "company".
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
What is the data in the database?  Maybe use var_dump() to print out the data.
Kim WalkerWeb Programmer/Technician

Commented:
Your entire table has only one record? select * from comp order by COMPANY selects the entire table named comp. And if there's only one records, what's the point of the order by clause?
HuaMin ChenProblem resolver

Commented:
Try to put

"select distinct ..."

Open in new window

in the SQL statement
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

F P
Top Expert 2015

Commented:
$SqlString = "SELECT DISTINCT(`COMPANY`), `TID` FROM `comp` ORDER BY `COMPANY`";

Open in new window


or

$SqlString = "SELECT `COMPANY`, `TID` FROM `comp` ORDER BY `COMPANY` LIMIT 1";

Open in new window


or

$SqlString = "SELECT DISTINCT(`COMPANY`), `TID` FROM `comp` ORDER BY `COMPANY`;";
$companies = $conn->query($SqlString);

while ($row_comp = $companies->fetch_array())
{
	$options .= "<option value='{$row_comp['TID']}'";

	if ($row_comp['TID'] == $row_survey_data['TID'])
 	{
		$options .= " selected='selected'";
	}

	$options .= ">{$row_comp['COMPANY']}</option>";
}

echo <<<HTML
<select id="company" name="Broker" width="200" style="width: 200px">
	{$options}
</select>
<br>
HTML;

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
If the recommendations from HuaMinChen or Frank Pennock work, you've got duplication in your database table, and "there is only one record" is not a true statement.  SELECT * is a code smell.  You might want to look at this database with something like phpMyAdmin and consider ways of eliminating and preventing duplication.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
I'm with Ray on this one - sounds like your data integrity is shot.

SELECT DISTINCT may work for you, but only if ALL the data you're selecting is distinct (hint: don't use SELECT *). If it is, then you have a problem with your Data. If it's not, then you'll still get duplicate entries in your dropdown.

Maybe post a sample of your data and we can advise accordingly.

Author

Commented:
Can I kill my partner?  Idiot kept updating the table without removing the previous records.  Day and a half trying to figure this out!!!!!!!!
Most Valuable Expert 2011
Top Expert 2016

Commented:
No, I don't think you can kill your partner.  But going forward, when you have a data-dependent problem, please post your test data here along with your expected output and your question.  This is called the SSCCE and it's a good habit to cultivate because it will really save you a lot of time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial