Hey I am having some trouble with my dropdown menu values that I retrieve from my database. I see two values instead of one.
$con = mysqli_connect("localhost","root","") ;
$myDB = mysqli_select_db($con, "test");
$dbEnc = mysqli_set_charset($con, 'utf8');
$sqlSELECT = mysqli_query($con, 'SELECT class FROM disastergroups');
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta http-equiv="content-type" content = "text/html; charset=utf-8"/>
</head>
<body>
<select name="test">
<option value="">Select...</option>
<?php while ($row1 = mysqli_fetch_array($sqlSELECT)): ;?>
<option><?php echo implode("\t", $row1); ?></option>
<?php endwhile;?>
</select>
<input type="submit" value="Submit Data">
</body>
</html>
The image below shows the duplicates in the dropdown menu... how do I fix this?
This is because the default of
resulttype
formysqli_fetch_array()
isint $resulttype = MYSQLI_BOTH
->mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )
. Change -to
Or, just use
mysqli_fetch_assoc()
so that you only get the 1 value, and you can remove the
implode()
->