Reputation:
I am trying to auto populate a dropdown menu with data from mysl db. The dropdown should display type inactive
and active
for user to pick. Below I have the table structure and the sql query from the php side. The dropdown is not displaying any values at all.
CREATE TABLE academy
(
academy_id int(11) not null auto_increment,
name varchar(25) not null,
type enum('INACTIVE','ACTIVE') DEFAULT 'ACTIVE' NOT NULL,
primary key (id),
);
php
Type: <select name="select_type">
<?php
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$dropDownQuery = "SELECT type FROM academy";
if ($result = $mysqli->query($dropDownQuery)) {
while ($row = $result->fetch_row()) {
$type = $row['type'];
echo "<option value=\"$type\">$type</option>";
}
}
?>
</select>
Upvotes: 0
Views: 91
Reputation:
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$table_name = "academy";
$column_name = "type";
echo "<select name=\"$column_name\"><option>Select one</option>";
$q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
$r = mysqli_query($mysqli, $q);
$row = mysqli_fetch_array($r);
//print_r($row);
$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
//print_r($enumList);
foreach($enumList as $value){
echo "<option value='$value'>$value</option>";
}
echo "</select></br>";
Upvotes: 0
Reputation: 1381
$type
is not defined in your code.
Replace $partner
with $type
$type = $row['type'];
echo "<option value=\"$type\">$type</option>";
Upvotes: 1
Reputation: 41
$dropDownQuery = "SELECT `type` FROM academy";
if ($result = $mysqli->query($dropDownQuery)) {
while ($row = $result->fetch_row()) {
$partner = $row['type'];
printf ('<option value="%s">%s</option>', $partner, $partner);
}
}
Upvotes: 0