Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

Getting 3 duplicate records in a drop down

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".
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What is the data in the database?  Maybe use var_dump() to print out the data.
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?
Try to put

"select distinct ..."

Open in new window

in the SQL statement
$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

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.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of breeze351
breeze351

ASKER

Can I kill my partner?  Idiot kept updating the table without removing the previous records.  Day and a half trying to figure this out!!!!!!!!
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.