0

I am having difficulty returning data from my database to a dropdown menu inside of a HTML form using PHP. Connection to my works and doesnt appear to be the issue but not returning data from the table to the dropdown. Any ideas? Thanks

<!DOCTYPE html>
<html>


<body>
<?php

try {
$conn = new PDO("mysql:host=$localhost;dbname=dbname", "root", "password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

$sql = "SELECT team_name FROM team;";
$result = mysqli_query($query,$conn) or var_dump(mysqli_error());
?>



<form action="">
First name:<br>
 <input type="text" name="firstname" value="First">
 <br>
 Last name:<br>
 <input type="text" name="lastname" value="Last">
 <br><br>

 <select  name ="name" required>
  <option selected disabled>--Select an option--</option>

  <?php 
  while ($row = $result->fetch_array()) {
    //echo "hi";
    $optionecho = "<option>" ;
    $optionecho .= $row['team_name'] ;
    $optionecho .=  "</option>";
    print($optionecho) ;
} ?>

</select>

2-b-able
  • 27
  • 6

3 Answers3

0

Give this a shot in your script:

while($row = $result->fetch(PDO::FETCH_ASSOC)){
    $option = "<option>";
    $option .= $row["team_name"];
    $option .= "</option>";

    echo $option;
}

Or:

<?php while($row = $result->fetch(PDO::FETCH_ASSOC)){ ?>
    <option><?= $row["team_name"]; ?></option>
<?php } ?>
GROVER.
  • 4,071
  • 2
  • 19
  • 66
0
<?php

$dbh = new PDO('mysql:host=localhost;dbname=populatedropdown', "root", "");

$query = $dbh->query("select * from position"); // Run your query

echo '<form action="populate.php" method="get">';
echo '<select name="populate">'; // Open your drop down box

echo '<option value="1">Select</option>';

// Loop through the query results, outputing the options one by one
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
   echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}

echo '</select>';// Close your drop down box


echo '<input type="submit" name="edit" value="Edit">';

echo '</form>';
?>
b2ok
  • 544
  • 6
  • 13
0

You are using PDO so better use following code to query data:

try {
$conn = new PDO("mysql:host=$localhost;dbname=dbname", "root", "password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

$sql = "SELECT team_name FROM team;";
?>
<form action="">
First name:<br>
 <input type="text" name="firstname" value="First">
 <br>
 Last name:<br>
 <input type="text" name="lastname" value="Last">
 <br><br>

 <select  name ="name" required>
  <option selected disabled>--Select an option--</option>

  <?php 
  foreach ($conn->query($sql) as $row) {
    //echo "hi";
    $optionecho = "<option>" ;
    $optionecho .= $row['team_name'] ;
    $optionecho .=  "</option>";
    print($optionecho) ;
} ?>
</select>
</form>