One client gets php/server-side error no one else gets

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
dominic998
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sun Apr 15, 2012 5:00 pm

One client gets php/server-side error no one else gets

Post by dominic998 » Sun Apr 15, 2012 5:30 pm

Hello,

I'm having a problem with a component I am writing, specifically a survey form meant for a company project. I'll present it in two parts. The main problem is presented in part 1. Those who want to know more can read on to part two.

*****Part 1*****

One of my colleagues, on completing the survey and clicking submit, gets the error:
Fatal error: Cannot use object of type JUser as array in /home/hpa/sites/dev/components/com_hrbpcalculator/models/hrbpcalculator.php on line 17
(this is with error_reporting -1 and display_errors on in .htaccess).

However, this is not an error I get myself when I complete this survey. Unless I am mistaken, this is a PHP, server-side problem, independent of anything an individual user might have (OS, browser) or be doing.

So, how is this possible? Have I overlooked something?

*****Part 2*****

A little more detail.

It is a survey component, with three pages. Starts on a form where users fill in personal info (name, organisation, etc.). When they submit the controller stores the post data in a jsession (not serialised), and they are sent to the next page, the survey itself. When they submit this the post data is taken, the user data is "get" from the jsession, and both are passed to a method in the model meant to store them in the database (Mysql).
This is the method it appears to be choking on (but for my colleague only). This is the method (part of models/hrbpcalculator.php):

public function store ($response, $user)
{

$db = JFactory::getDBO();

$title = $user['title']; // This is line 17, alluded to in the error above
$fname = $user['fname'];
$sname = $user['sname'];
$org = $user['org'];
$occ = $user['occ'];
$email = $user['email'];
$query = "INSERT INTO `#__hrbp_survey_response` "
. "(survey_id, title, first_name, surname, organisation, "
. "job_title, email) VALUES (1, '$title', '$fname', "
. "'$sname', '$org', '$occ', '$email')";
$db->setQuery($query);
$db->query();

$srvy_resp_id = $db->insertid();
$count = count($response);

for ($i = 1; $i <= $count; $i++) {
$query = "INSERT INTO `#__hrbp_response_int` "
. "(survey_response_id, question_id, response) "
. "VALUES (".$srvy_resp_id.", ".$i.", "
.$response[$i].")";
$db->setQuery($query);
$db->query();
}

return true;

}

$response being the post data from the survey, $user being the post data from the user form (here I'm taking out the title, first name, surname, etc.). Is $user a special variable in php or something?

At no point in my component have I used JUser (to my knowledge, unless another object I am calling uses it).

I have tested this myself and get no error on my own browser (Iceweasel/Firefox on Debian GNU/Linux), nor any other browser I have tried (IE, Chrome/Chromium, Epiphany), nor any other OS (XP, Vista). My colleague has Firefox 5 on Vista (a combination I have tested elsewhere, which worked).

You can see why it has really stumped me. Any suggestions would be appreciated. Please say if I've omitted any useful information.

Thanks,

Dom

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: One client gets php/server-side error no one else gets

Post by stutteringp0et » Sun Apr 15, 2012 11:14 pm

In your component, at any point did you use JFactory::getUser()?

If so, that method returns a JUser object.

JUser is an object - not an array, so you must access its properties as you would an object.

The strange thing is that you're accessing properties not present in the JUser object.

Which particular line of the the code above is line 17?
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

dominic998
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sun Apr 15, 2012 5:00 pm

Re: One client gets php/server-side error no one else gets

Post by dominic998 » Mon Apr 16, 2012 8:15 am

Hi, thanks for the reply.

No I didn't, I haven't used it at all in my component.

The line commented above ($title = $user['title']) is line 17. $user should just be the POST array, which should be accessible in this way ('title' refers to the name of one of the html input elements in the form).

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: One client gets php/server-side error no one else gets

Post by stutteringp0et » Mon Apr 16, 2012 4:01 pm

I think I know what's going on here.

The $user var is retrieved automatically by the system if you're logged in when you submit the form.

You should be retrieving your POST vars using JRequest.

Here's how I think your store function should look using JRequest:

Code: Select all

public function store ($response, $user)
{
    $db = JFactory::getDBO();

    $title = JRequest::getString('title','');
    $fname = JRequest::getString('fname','');
    $sname = JRequest::getString('sname','');
    $org = JRequest::getString('org','');
    $occ = JRequest::getString('occ','');
    $email = JRequest::getString('email','');
    $query = "INSERT INTO `#__hrbp_survey_response` "
    . "(survey_id, title, first_name, surname, organisation, "
    . "job_title, email) VALUES (1, '$title', '$fname', "
    . "'$sname', '$org', '$occ', '$email')";
    $db->setQuery($query);
    $db->query();

    $srvy_resp_id = $db->insertid();
    $count = count($response);

    for ($i = 1; $i <= $count; $i++) {
        $query = "INSERT INTO `#__hrbp_response_int` "
        ."(survey_response_id, question_id, response) "
        ."VALUES (".$srvy_resp_id.", ".$i.", "
        .$response[$i].")";
        $db->setQuery($query);
        $db->query();
    }

    return true;
}
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

dominic998
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Sun Apr 15, 2012 5:00 pm

Re: One client gets php/server-side error no one else gets

Post by dominic998 » Tue Apr 17, 2012 9:55 am

I've finally found the problem (or at least I think I have).

I had overridden (if that's the correct terminology, redefined maybe) the default display() method in the view, but my new method wasn't compatible with the parent method. (I had done "public function display()", rather than "public function display($tpl=null)", and called parent::display() without the $tpl parameter). Which evidently led to some unpredictable results.

I'll need to investigate further to be certain this was the problem, but for now it looks like it was.

Thanks for you help stutteringpoet,

Dominic


Locked

Return to “Joomla! 2.5 Coding”