herghost Posted April 5, 2009 Share Posted April 5, 2009 I have this code which is simply meant to display users from a database: <?php session_start(); require 'include/database.php'; $nuser=$_SESSION['user']; $auser=$_SESSION['admin']; if($nuser){ $userfinal=$nuser; }elseif($auser){ $userfinal=$auser; } if(isset($userfinal)){ $Members = mysql_query("SELECT username FROM user WHERE level ='1'") or die(mysql_error()); $numRowsMembers = mysql_num_rows($Members); } ?> <table border="1"> <?php for($count = 1; $count <= $numRowsMembers; $count++) { $name = mysql_fetch_array($Members); ?> <tr> <?php echo '<td><a href="member_profile.php?username=' . $name['user'] . '">' . $name['user'] . '</a></td>'; ?> </tr> <?php } ?> </table> The fields in my database called user are id, username, password, level id is autoincrement, username and password are set at registration and level is default 1. The error messages I get are: Notice: Undefined index: admin in C:\wamp\www\fanjunky\members.php on line 5 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 Notice: Undefined index: user in C:\wamp\www\fanjunky\members.php on line 28 What am I not doing correctly? Thanks Link to comment https://forums.phpfreaks.com/topic/152683-solved-undefined-index-help/ Share on other sites More sharing options...
corbin Posted April 5, 2009 Share Posted April 5, 2009 user should be username I'm guessing since you're selecting a column named username. By the way, instead of your $count = 1; ... stuff, you can just use a simple while loop. while($array = mysql_fetch_assoc($query_resource)) { } for example. Link to comment https://forums.phpfreaks.com/topic/152683-solved-undefined-index-help/#findComment-801815 Share on other sites More sharing options...
Axeia Posted April 6, 2009 Share Posted April 6, 2009 Undefined index means that the value isn't set. At the bottom of your code you do a isset($userfinal).. so that line would never given an undefined index error message. Basically you always got to make sure a value is set, you might want to wrap the entire thing in a, if( isset( $_SESSION['user'] ) && isset( $_SESSION['admin'] ) { edit If you always want a default value to be used you could a ternary operator like this: <?php $username = isset( $_SESSION['user'] ) ? $_SESSION['user'] : 'default'; ?> That would put the username from the session in $username if its set or default if it's not. Link to comment https://forums.phpfreaks.com/topic/152683-solved-undefined-index-help/#findComment-801920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.