-2

im having trouble with my code. I'm trying to populate a drop down list from my database but the list is showing empty space instead of database entries. I would be very grateful if anyone could help me.

<?php
include("db_config.php");
$sql="SELECT * FROM brojevi";
$result=mysqli_query($connection,$sql);
echo "<select name=\"id\" size=\"1\">";
echo "<option value=\"choose\">-choose-</option>";
while($row=mysqli_fetch_array($result)){
unset($id,$broj);
$id=$row['id'];
$broj=$row['brojevi'];
echo "<option value=\"$id\">$broj</option>";
}
echo "</select>";
?>

Here is the table( its a simple one cause im just making this for practice )

 CREATE TABLE `brojevi` (
 `id` int(11) NOT NULL auto_increment,
 `broj` int(11) NOT NULL,
 PRIMARY KEY  (`id`)
 );

INSERT INTO `brojevi` (`id`, `broj`) VALUES
(1, 1),
(2, 2);
2
  • 1
    You should look into the MVC Design Pattern. Commented Jan 10, 2015 at 22:25
  • How does the table look? Commented Jan 10, 2015 at 22:25

1 Answer 1

1

You are messing up your single and double quotes. Try this:

<?php
include("db_config.php");
$sql="SELECT * FROM brojevi";
$result=mysqli_query($connection,$sql);
echo '<select name="dropdown" size="1">';
echo '<option value="choose">-choose-</option>';
while($row=mysqli_fetch_array($result)){
    $id = $row['id'];
    $broj = $row['brojevi'];
    echo '<option value="' . $id . '">' . $broj . '</option>';
}
echo '</select>';
?>

When using html tags inside your echo, try to use single quotes for the echo and double quotes for html stuff like values,id's,classes etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.