squiblo Posted August 18, 2009 Share Posted August 18, 2009 with this code, if the user is not logged in, i get this notice... Notice: Undefined index: mycompany in /customers/squiblo.com/squiblo.com/httpd.www/index.php on line 245 this is the code... <?php include("checklogin.php"); $company = $_SESSION['mycompany']; $query = mysql_query("SELECT * FROM members WHERE username='$company'"); if (mysql_num_rows($query)==0){ die; }else{ $row = mysql_fetch_assoc($query); $location = $row['image']; if($location == ""){ echo "<img src ='/profileimages/box.png' width='110' height='110' border='0'></a>"; }else{ echo "<img src ='$location' width='110' height='110' border='0'>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/ Share on other sites More sharing options...
bubbasheeko Posted August 18, 2009 Share Posted August 18, 2009 $_SESSION['mycompany']; Have you "started the session" in this file? (ie. <?php session_start(); ?> ) Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901435 Share on other sites More sharing options...
squiblo Posted August 18, 2009 Author Share Posted August 18, 2009 yes, i have session_start(); at the beginning of each page Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901444 Share on other sites More sharing options...
thebadbad Posted August 18, 2009 Share Posted August 18, 2009 You can eliminate the notice by checking if the element is set: $company = isset($_SESSION['mycompany']) ? $_SESSION['mycompany'] : 'default_value'; Just decide what you want to do if it's not set. Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901451 Share on other sites More sharing options...
squiblo Posted August 18, 2009 Author Share Posted August 18, 2009 ok now im a little confused Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901455 Share on other sites More sharing options...
thebadbad Posted August 19, 2009 Share Posted August 19, 2009 In my snippet I set $company to $_SESSION['mycompany'] if it's set, else to default_value. Do you still want to query the database if the user isn't logged in? Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901467 Share on other sites More sharing options...
squiblo Posted August 19, 2009 Author Share Posted August 19, 2009 sorry im new to php and i have not come across default_value yet and dont really understand, if im not logged in the image does not appear, but when i log in an image appears correctly. Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901472 Share on other sites More sharing options...
thebadbad Posted August 19, 2009 Share Posted August 19, 2009 Okay, default_value was just a string I typed in. You could then substitute it with your own default value for $company if you had one. But if the script is working as intended, and you just want to get rid of the notice, change $company = $_SESSION['mycompany']; to $company = isset($_SESSION['mycompany']) ? $_SESSION['mycompany'] : ''; or if you prefer the regular syntax: if (isset($_SESSION['mycompany'])) { $company = $_SESSION['mycompany']; } else { $company = ''; } Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901481 Share on other sites More sharing options...
squiblo Posted August 19, 2009 Author Share Posted August 19, 2009 that has worked perfect thanks Link to comment https://forums.phpfreaks.com/topic/170911-solved-unwanted-notice/#findComment-901483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.