0

I am trying to make an select field for data according to my database. So, basically, I want it to have a dropdown menu for all the users in my database. So like instead of doing:

<select>
  <option>1st User in Db</option>
  <option>2nd User in Db</option>
</select>

It just automatically shows all.

I tried fetching a $row['uname'], and setting it as $user, but then it only shows MY user in the dropdown.

Example: http://prntscr.com/dwksy

It only shows my account, admin, instead of all the other ones. Anyone know how to do this? Thanks!

2
  • 1
    Can we see the SQL you're using to query the database? And the PHP generating the list? Commented Aug 16, 2012 at 20:17
  • Sounds like you need a different query to get the right info back. Commented Aug 16, 2012 at 20:17

2 Answers 2

1
<?php

session_start();
include 'config.php';
$user = $_SESSION['username'];
$qry=("SELECT `rank`, `uname` FROM users WHERE uname='$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row["rank"]

if ($_SESSION['loggedin'] == true) {
    if ($rank >= 3) {
        $qry=("SELECT `rank`, `uname` FROM users");
        $result=mysql_query($qry);

        echo "
        <form action='' method='POST'>
        <select>";

        while ($row = mysql_fetch_assoc($result)) {
            $rank = $row['rank'];
            $users = $row['uname'];
            $lol = ucwords($users);

            echo "<option>$lol</option>";
        }

        echo "</select>
        </form>
        ";
      } else {
          echo "Your level is not 3, but you are logged in.";
      }
  } else {
     echo "Please login.";
  }
Sign up to request clarification or add additional context in comments.

13 Comments

Okay, now it is getting tangled with my if statements
Do you want to add them to the question?
Added !! :) This has to be atleast 15 chars so iosfjeorjferf
I think that should do it. There are a few things you should look at, though, like making sure your query is returning data; and you might want to look into converting it to use PDO, too.
That's the code that doesn't work and only shows MY user. I want it to show every user in the database. Can you fix up my code? When I add the wait loop, it just gives me unexpected T_ELSE, because it gets tangled with my other codes.
|
1

Your query only fetches the user recorded in $_SESSION. Drop the WHERE clause to get all users in the result, then read them with @andrewsi's loop and spit out one <option> for each user returned:

$qry=("SELECT `rank`, `uname` FROM users");

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.