Link to home
Start Free TrialLog in
Avatar of sittinDuck
sittinDuck

asked on

PHP Notice: Undefined index

Hi experts,

I'm getting these erros all the time:

[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: statistics_tablereservations in /var/www/html/dev.housekeeping.dk/classes/user.php on line 112, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: newsletter_create in /var/www/html/dev.housekeeping.dk/classes/user.php on line 118, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: edit_addressbook in /var/www/html/dev.housekeeping.dk/classes/user.php on line 125, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: delete_addressbook in /var/www/html/dev.housekeeping.dk/classes/user.php on line 126, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: statistics_tablereservations in /var/www/html/dev.housekeeping.dk/classes/user.php on line 112, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: newsletter_create in /var/www/html/dev.housekeeping.dk/classes/user.php on line 118, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: edit_addressbook in /var/www/html/dev.housekeeping.dk/classes/user.php on line 125, referer: http://dev.housekeeping.dk/dashboard.php
[Wed Oct 19 20:38:55 2011] [client 87.72.126.215] PHP Notice:  Undefined index: delete_addressbook in /var/www/html/dev.housekeeping.dk/classes/user.php on line 126, referer: http://dev.housekeeping.dk/dashboard.php

Can someone help me fix these? I have attached the class.

Thanks in advance! :)
<?php

class User {

	// User fields
	public $user_id;
	public $user_name;
	public $user_full_name;
	public $user_accessgroup;
	public $user_email;
	public $user_cc_mobil;
	public $user_mobil;
	public $user_image;
	
	// Accessgroup fieds
	public $access_create_user;
	public $access_edit_user;
	public $access_delete_user;
	public $access_accessgroups;
	public $access_tablebooking_view;
	public $access_tablebooking_create;
	public $access_tablebooking_edit;
	public $access_tablebooking_delete;
	public $access_template_create;
	public $access_template_edit;
	public $access_template_delete;
	public $access_guestlist;
	public $access_all_logins;
	public $access_latest_logins;
	public $access_activity;
	public $access_folderView;
	public $access_folderCreate;
	public $access_folderRename;
	public $access_folderDelete;
	public $access_fileView;
	public $access_fileUpload;
	public $access_fileRename;
	public $access_fileDelete;
	public $access_payment_queue_info;
	public $access_statistics_tablereservations;
	public $access_ZendeskSupport;
	public $accesss_hostess_tools;
	public $access_add_edit_products;
	public $access_delete_products;
	public $access_newsletter_access;
	public $access_newsletter_create;
	public $access_newsletter_send;
	public $access_members_access;
	public $access_members_edit;
	public $access_members_blacklist;
	public $access_members_sensitive_data;
	public $access_view_addressbook;
	public $access_edit_addressbook;
	public $access_delete_addressbook;
	
	// Loads the user data
	
	function user($userid){
		global $db;
		
		if ($userid == 'me') { $userid = $_SESSION['MM_UserId']; }
		
			$sql="select * from users where id=".$userid;
			$rows=$db->q($sql);
			if(sizeof($rows)>0){
				$row=$rows[0];
				
				$this->user_id = $row["id"];
				$this->user_name = htmlspecialchars($row["username"]);
				$this->user_full_name = htmlspecialchars($row["full_name"]);
				$this->user_accessgroup = htmlspecialchars($row["accessgroup"]);
				$this->user_email = htmlspecialchars($row["email"]);
				$this->user_cc_mobil = htmlspecialchars($row["cc_mobil"]);
				$this->user_mobil = htmlspecialchars($row["mobil"]);
				$this->user_image = htmlspecialchars($row["image"]);
				
				// Access group
				$this->access($this->user_accessgroup);
			}
	}
	
	// Access check
	
	function access($accessgroup){
		global $db;
		
			$sql="select * from accessgroups where id=".$accessgroup;
			$rows=$db->q($sql);
			if(sizeof($rows)>0){
				$row=$rows[0];
				
				$this->access_create_user = $row["create_user"];
				$this->access_edit_user = $row["edit_user"];
				$this->access_delete_user = $row["delete_user"];
				$this->access_accessgroups = $row["accessgroups"];
				$this->access_tablebooking_view = $row["tablebooking_view"];
				$this->access_tablebooking_create = $row["tablebooking_create"];
				$this->access_tablebooking_edit = $row["tablebooking_edit"];
				$this->access_tablebooking_delete = $row["tablebooking_delete"];
				$this->access_template_create = $row["template_create"];
				$this->access_template_edit = $row["template_edit"];
				$this->access_template_delete = $row["template_delete"];
				$this->access_guestlist = $row["guestlist"];
				$this->access_all_logins = $row["all_logins"];
				$this->access_latest_logins = $row["latest_logins"];
				$this->access_activity = $row["activity"];
				$this->access_fileView = $row["fileView"];
				$this->access_fileUpload = $row["fileUpload"];
				$this->access_fileRename = $row["fileRename"];
				$this->access_fileDelete = $row["fileDelete"];
				$this->access_payment_queue_info = $row["payment_queue_info"];
				$this->access_statistics_tablereservations = $row["statistics_tablereservations"];
				$this->access_ZendeskSupport = $row["ZendeskSupport"];
				$this->accesss_hostess_tools = $row["hostess_tools"];
				$this->access_add_edit_products = $row["add_edit_products"];
				$this->access_delete_products = $row["delete_products"];
				$this->access_newsletter_access = $row["newsletter_access"];
				$this->access_newsletter_create = $row["newsletter_create"];
				$this->access_newsletter_send = $row["newsletter_send"];
				$this->access_members_access = $row["members_access"];
				$this->access_members_blacklist = $row["members_blacklist"];
				$this->access_members_edit = $row["members_edit"];
				$this->access_members_sensitive_data = $row["members_sensitive_data"];
				$this->access_view_addressbook = $row["view_addressbook"];
				$this->access_edit_addressbook = $row["edit_addressbook"];
				$this->access_delete_addressbook = $row["delete_addressbook"];	
			}
		}
		
	// User photo
		
	public function user_photo($id) {
		global $db;
		
		$sql = "SELECT image FROM users WHERE id = '".$id."' LIMIT 1";
		$rows=$db->q($sql);
		if(sizeof($rows)>0){
		if($rows[0]["image"]){
			return $rows[0]["image"];
		}
		else{
			return "no_profile.jpg";
		}
	}
	else{
		return "";
	}
		
	}
}
?>

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Can youpost your table structure (something like mysql CREATE statement obtained by exporting your table)?
ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See http://www.laprbass.com/RAY_temp_sittinDuck.php
<?php // RAY_temp_sittinDuck.php.php
echo "<pre>";


// SHOW WHY UNDEFINED INDEX NOTICES HAPPEN


// SET THE DEFAULT ERROR_REPORTING() TO SUPPRESS NOTICE MESSAGES
error_reporting(E_ALL ^ E_NOTICE);

// DEFINE SOME DATA IN AN ARRAY
$things = array
( 'one' => 'ONE'
, 'two' => 'TWO'
, 'tre' => 'THREE'
)
;

// SHOW THE ARRAY
var_dump($things);

// ACCESS A NON-EXISTENT ELEMENT
var_dump($things["for"]);

// SET THE ERROR REPORTING TO PRODUCE ALL THE MESSAGES
error_reporting(E_ALL);

// ACCESS AN EXISTING ELEMENT
var_dump($things["tre"]);

// ACCESS A NON-EXISTENT ELEMENT
var_dump($things["for"]);

Open in new window

In a very over-simplified way, the "Undefined Index" means the code is trying to access an index in the array that isn't actually there.  To avoid these errors, you would need to do something like this:
if(isset($row["statistics_tablereservations"])) {
    $this->access_statistics_tablereservations = $row["statistics_tablereservations"];
}
else {
    // if the index is not defined, the value returned will be null anyway.
    $this->access_statistics_tablereservations = null;
}

Open in new window


Because this code is setting object members ($object->member), passing invalid values can lead to unexpected behavior.  It would be difficult, to say the least, for anyone to "fix" your code without potentially causing problems.
One more thought (taking into consideration the difficulties I mentioned before).  Something I've done in the past is to make "field mappings", which is basically an array that matches object members to row values.
        $fieldMappings = array(
                'access_create_user'    => "create_user",
                'access_edit_user'      => "edit_user",
                'access_delete_user'    => "delete_user"
        );
        foreach($fieldMappings as $objectMember => $rowIndex)
        {
                $val = null;
                if(isset($row[$rowIndex])) {
                        $val = $row[$rowIndex];
                }
                $this->$objectMember = $val;
        }

Open in new window

Avatar of sittinDuck
sittinDuck

ASKER

Ah, of course, I must have been very tired when I made the class hehe :-) Thank you all.