Jump to content

Not returning rows into a select field


MSUK1

Recommended Posts

Hello, i am trying to get my form to list usernames ascending as an option, but the select field just returns blank.

 

Here is my code:

 

/**
* editUserForm - Displays the users database table in
* a nicely formatted field table.
*/

function editUserForm(){
   $q = "SELECT * "
       ."FROM ".TBL_USERS." ORDER BY username ASC";
   $result = $database->query($q);

while ($row = mysql_fetch_assoc($result)) 
{ 
echo '<option value="'.$row["username"].'">'.$row["username"].'</option>';
}
}

 

              <form action="/" method="post">

                <div class="column">
                  <p>
                    <select name="user" id="user" placeholder="Select A User To Edit" class="{validate:{required:true}}">
                      <option>Select A User To Edit</option>
                      <? editUserForm(); ?>
                    </select>
                  </p>

                <div class="action_bar">
                  <input type="submit" class="button blue" value="Submit Post" />
                  <a href="#modal" class="modal button">Cancel</a>
                </div>

              </form>

 

All help is appreciated :)

Link to comment
Share on other sites

should add some error checking, rather than assuming that the query will ALWAYS return one or more results.

 

if(mysql_num_rows($result) > 0) 
{
while ($row = mysql_fetch_assoc($result)) 
{ 
echo '<option value="'.$row["username"].'">'.$row["username"].'</option>';
}
} else {
   echo '<option>No Usernames</option>';
}

 

EDIT: It's probably not displaying any usernames because either the query isn't working as expected, or you have no records in that table.

Link to comment
Share on other sites

              <form action="/" method="post">

                <div class="column">
                  <p>
                    <select name="user" id="user" placeholder="Select A User To Edit">
                      <option>Select A User To Edit</option>
                                          </select>
                  <span class="SC_red">Required - Please select the user to edit.</span>
                  </p>
                  
                  <p>
                    <select name="updlevel" id="updlevel" placeholder="Select A User Role">
                      <option>Select A User Role</option>
                      <option value="9">Admin</option>
                      <option value="1">Leader</option>
                    </select>
                  <span class="SC_grey">Optional - Please choose a new user role.</span>
                  </p>

                  <p>
                  	<input type="password" id="newpass" placeholder="password" name="newpass" />
                    <span class="SC_grey">Optional - Please make a copy of this password.</span>
                  </p>
                  
                </div>

                <div class="action_bar">
                  <input type="submit" class="button blue" value="Submit Post" />
                  <a href="#modal" class="modal button">Cancel</a>
                </div>

              </form>

 

this is chrome's view source of the form i think it might be a conflict of code so i altered my scripts variable to ensure no conflicts i'll past full source now from raw code

 

<?php
include("system/include/session.php");

/**
* displayUsers - Displays the users database table in
* a nicely formatted html table.
*/
function displayUsers(){
   global $database;
   $q = "SELECT username,userlevel,email,timestamp "
       ."FROM ".TBL_USERS." ORDER BY userlevel DESC,username";
   $result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   echo "<table>\n";
   echo "<thead>
   <tr>
   <th>Username</th>
   <th><a href=\"#\" class=\"button tooltip\" original-title=\"9 = Admin :: 1 = Leader\">User Level<span class=\"icon16 question\"></span></a></th>
   <th>Email</th>
   <th>Edit</th>
   </tr>
   </thead>\n";
   for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
      $ulevel = mysql_result($result,$i,"userlevel");
      $email  = mysql_result($result,$i,"email");

      echo "<tbody><tr><td>$uname</td><td>$ulevel</td><td>$email</td><td><a href=\"#edituser\" class=\"button modal plain\">Edit</a></td></tr></tbody>\n";
   }
   echo "</table><br>\n";
}

/**
* editUserForm - Displays the users database table in
* a nicely formatted field table.
*/

function editUserForm(){
   $sq = "SELECT * "
       ."FROM ".TBL_USERS." ORDER BY username ASC";
   $selectresult = $database->query($sq);

while ($srow = mysql_fetch_assoc($selectresult)) 
{ 
echo '<option value="'.$srow["username"].'">'.$srow["username"].'</option>';
}
}

if($session->logged_in){

?>

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="viewport" content="width=1024px, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    
    <title></title>
    
    <!-- Stylesheets -->
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/icons.css" />
    <link rel="stylesheet" href="css/formalize.css" />
    <link rel="stylesheet" href="css/checkboxes.css" />
    <link rel="stylesheet" href="css/sourcerer.css" />
    <link rel="stylesheet" href="css/jqueryui.css" />
    <link rel="stylesheet" href="css/tipsy.css" />
    <link rel="stylesheet" href="css/calendar.css" />
    <link rel="stylesheet" href="css/tags.css" />
    <link rel="stylesheet" href="css/selectboxes.css" />
    <link rel="stylesheet" href="css/960.css" />
    <link rel="stylesheet" href="css/main.css" />
    <link rel="stylesheet" media="all and (orientation:portrait)" href="css/portrait.css" />
    <link rel="apple-touch-icon" href="apple-touch-icon-precomposed.png" />
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/excanvas.js"></script>
    <![endif]-->
    
    <!-- JavaScript -->
    <script src="js/jquery.min.js"></script>
    <script src="js/jqueryui.min.js"></script>
    <script src="js/jquery.cookies.js"></script>
    <script src="js/jquery.pjax.js"></script>
    <script src="js/formalize.min.js"></script>
    <script src="js/jquery.metadata.js"></script>
    <script src="js/jquery.validate.js"></script>
    <script src="js/jquery.checkboxes.js"></script>
    <script src="js/jquery.chosen.js"></script>
    <script src="js/jquery.fileinput.js"></script>
    <script src="js/jquery.datatables.js"></script>
    <script src="js/jquery.sourcerer.js"></script>
    <script src="js/jquery.tipsy.js"></script>
    <script src="js/jquery.calendar.js"></script>
    <script src="js/jquery.inputtags.min.js"></script>
    <script src="js/jquery.wymeditor.js"></script>
    <script src="js/jquery.livequery.js"></script>
    <script src="js/jquery.flot.min.js"></script>
    <script src="js/application.js"></script>
  </head>
  
  <body>

  <!-- Primary navigation -->
    <nav id="primary">
      <ul>
        <li>
          <a href="dashboard">
            <span class="icon32 dashboard"></span>
            Dashboard
          </a>
        </li>
        <li>
          <a href="members">
            <span class="icon32 group"></span>
            My Users
          </a>
        </li>
        <li>
          <a href="information">
            <span class="icon32 info"></span>
            Information
          </a>
        </li>
        <li>
          <a href="settings">
            <span class="icon32 cog"></span>
            My Settings
          </a>
        </li>
        <? if($session->isAdmin()){ ?>
        <li class="active">
          <a href="admin">
            <span class="icon32 tools"></span>
            Admin
          </a>
        </li>
        <? } ?>
        <li class="bottom">
          <a href="system/process.php">
            <span class="icon32 quit"></span>
            Log out
          </a>
        </li>
      </ul>    
     </nav>
        
    <section id="maincontainer">
      <div id="main" class="container_12">

        <? if($session->isAdmin()){ ?>
      
            <h1>Admin Area</h1>
             <p>Welcome to the Admin Area. Only a select few can see this portal. Here you may
             add and remove leaders, edit permissions and manage the system.</p>

        <div class="column-left">
         <div class="box">
          <div class="box-header">
            <span class="icon16 tools"></span><h1>MZCC Leaders</h1>
          </div>

          <div class="box-content">
            <?
            displayUsers();
            ?>
          </div>
	 </div>        

         <div class="box">
          <div class="box-header">
            <span class="icon16 tools"></span><h1>MZCC Leaders</h1>
          </div>

          <div class="box-content">
            <?
            displayUsers();
            ?>
          </div>
	 </div>        
        </div>

        <div class="column-right">
         <div class="box">
          <div class="box-header">
            <span class="icon16 tools"></span><h1>Admin Area</h1>
          </div>

          <div class="box-content">
            <h3>Add A New User</h3>
            
            <?
		if(isset($_SESSION['regsuccess'])){
		   /* Registration was successful */
		   if($_SESSION['regsuccess']){
			  echo "<h3>Registered!</h3>";
			  echo "<p>Thank you <strong>".$_SESSION['reguname']."</strong>, has been added to the database.</p>";
		   }
		   /* Registration failed */
		   else{
			  echo "<h1>Registration Failed</h1>";
			  echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
				  ."could not be completed.<br>Please try again at a later time.</p>";
		   }
		   unset($_SESSION['regsuccess']);
		   unset($_SESSION['reguname']);
		}
		?>

              <form action="system/process.php" method="post">

                <div class="column">
                  
                  <p>
                    <select name="userlevel" id="userlevel" placeholder="Select A User Role">
                      <option>Leader</option>
                    </select>
                  <span class="SC_grey">Default user role: Leader</span>
                  </p>

                  <p>
                  	<input type="text" id="user" placeholder="Username" name="user" />
                  </p>

                  <p>
                  	<input type="password" id="pass" placeholder="password" name="pass" />
                  </p>

                  <p>
                  	<input type="text" id="email" placeholder="Email Address" name="email" />
                  </p>
                  
                  <input type="hidden" name="subjoin" value="1">
                  
                </div>

                <div class="action_bar">
                  <input type="submit" class="button blue" value="Add New User" />
                </div>

              </form>

          </div>
         </div>

         <div class="box">
          <div class="box-header">
            <span class="icon16 tools"></span><h1>Admin Area</h1>
          </div>

          <div class="box-content">
            <h3>Edit A  Leader</h3>

              <form action="/" method="post">

                <div class="column">
                  <p>
                    <select name="user" id="user" placeholder="Select A User To Edit">
                      <option>Select A User To Edit</option>
                      <? editUserForm(); ?>
                    </select>
                  <span class="SC_red">Required - Please select the user to edit.</span>
                  </p>
                  
                  <p>
                    <select name="updlevel" id="updlevel" placeholder="Select A User Role">
                      <option>Select A User Role</option>
                      <option value="9">Admin</option>
                      <option value="1">Leader</option>
                    </select>
                  <span class="SC_grey">Optional - Please choose a new user role.</span>
                  </p>

                  <p>
                  	<input type="password" id="newpass" placeholder="password" name="newpass" />
                    <span class="SC_grey">Optional - Please make a copy of this password.</span>
                  </p>
                  
                </div>

                <div class="action_bar">
                  <input type="submit" class="button blue" value="Submit Post" />
                  <a href="#modal" class="modal button">Cancel</a>
                </div>

              </form>

          </div>
         </div>
        </div>
        
        <div class="clear"></div
        
        <? } ?>

        <? if(!$session->isAdmin()){ ?>
        
        <div class="box grid_12 alpha">
          <div class="box-header">
            <span class="icon16 alert"></span><h1>Permission Denied</h1>
          </div>

          <div class="box-content">
            <h1>Permission Denied</h1>
             <p>You do not have sufficient privilidges to view this page.</p>
          </div>
        </div>
        
        <? } ?>

      </div>
    </section>
  </body>
</html>

<?
}
else{
header('Location:  http://domain/login.php');
}
?>

Link to comment
Share on other sites

You need to have php's error_reporting set to E_ALL (or even better a -1) and display_errors set to ON in your master php.ini on your development system so that php will report and display all the errors it detects. Your $database variable doesn't exist in several of your function definitions and each reference to it would be throwing php errors to alert you to the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.