1

I have a website in Yii. It was working perfectly. But, when I upgrade MySql I suffer from some errors.

1.) date(): It is not safe to rely on the system's timezone settings.

But I have solved it by defining timezone.

2.) Undefined index: Registration.

I cant solve it. so, What do I do? My code is as below:

public function actionIndex() {
     $model = new Supplier('search');
     $model1 = new Registration('search');
     $model->unsetAttributes();
     $model1->unsetAttributes();

     if (isset($_REQUEST['Supplier'] , $_REQUEST['Registration']))
         $model->setAttributes($_REQUEST['Supplier']);
         $model1->setAttributes($_REQUEST['Registration']); // here is the error.

     $this->render('admin', array(
         'model' => $model,
         'model1' => $model1,
     ));
 }

Here, If I define $_REQUEST['Registration'] in my URL then it will work but, I cant do that because it is everywhere in my site. And error occurs after upgrade Mysql. So, What should I do?

Thanks,

Dharmesh patel
  • 654
  • 1
  • 12
  • 23
  • 1
    Are you sure it was only an upgrade of MySQL and not also PHP, or at least its php.ini? Both, the missing [date.timezone](http://docs.php.net/datetime.configuration#ini.date.timezone) configuration and the missing REQUEST parameter, have virtually nothing to do with MySQL. – VolkerK Mar 08 '16 at 07:44

2 Answers2

1

Ok i have no idea what the code should do, but the first Thing i notice:

if (isset($_REQUEST['Supplier'] , $_REQUEST['Registration']))
     $model->setAttributes($_REQUEST['Supplier']);
     $model1->setAttributes($_REQUEST['Registration']); // here is the error

this part is missing curly braces.

if (isset($_REQUEST['Supplier'] , $_REQUEST['Registration'])){
     $model->setAttributes($_REQUEST['Supplier']);
     $model1->setAttributes($_REQUEST['Registration']); // here is the error
}

will make much more sense. Else it will try to set Registration even if the isset is false.

Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • I appreciate you, But I think the error is different because when I try below code then also error occured: `
    renderPartial('/layouts/navigation');?>
    ` it shows `Undefined index: sub`
    – Dharmesh patel Mar 08 '16 at 08:42
  • 2
    same Problem, $_REQUEST['sub'] is not set, and not checked here. You should check the page where the requests are set – Doktor OSwaldo Mar 08 '16 at 08:44
  • In this case should I define `$_REQUEST['sub']` previouly? It will cause so much problem because there are around thousand of time in my site !!! And previously it was working. – Dharmesh patel Mar 08 '16 at 08:47
  • 2
    @Dharmesh By "previously it was working" I'm pretty sure you mean **previously error reporting was turned off and we've been ignoring this problem for years.** Get used to working with error reporting on. – deceze Mar 08 '16 at 08:58
  • @ deceze Yes you are right, when I use `error_reporting(0)` in my index.php file then it works fine... So, what do I do? work with `error_reporting(0)` or any thing else? Please comment on it. – Dharmesh patel Mar 08 '16 at 09:03
  • 3
    @Dharmesh **Fix. Your. Errors.** Don't ignore them. Again: get used to working with error reporting on. It's there to point out potential problems to you. Fix potential problems. – deceze Mar 08 '16 at 09:05
  • 1
    You should definitly fix the Errors, and add isset checks to your $_REQUEST variables. if it's not a critical variable you can still continue to run your code this way. But without the error reporting you will sooner or later get sideeffects! – Doktor OSwaldo Mar 08 '16 at 09:19
1

Your error here:

if (isset($_REQUEST['Supplier'], $_REQUEST['Registration']))
    $model->setAttributes($_REQUEST['Supplier']);
    $model1->setAttributes($_REQUEST['Registration']); // here is the error.

It's the same as:

if (isset($_REQUEST['Supplier'], $_REQUEST['Registration'])) {
    $model->setAttributes($_REQUEST['Supplier']);
}

$model1->setAttributes($_REQUEST['Registration']); // here is the error.

You are trying to get $_REQUEST['Registration'] even it is not set. So, to fix it, change your code:

if (isset($_REQUEST['Supplier'], $_REQUEST['Registration'])) {
    $model->setAttributes($_REQUEST['Supplier']);
    $model1->setAttributes($_REQUEST['Registration']);
}

It is very often error for anybody who love to ignore curly braces when they have one statement for the if block. I strongly recommend use curly braces in any cases, even you have only one statement under if block.

Incorrect approach:

if (true)
    do_something();

Correct approach:

if (true) {
    do_something();
}

If you use incorrect approach, you will have a lot of situations when you are adding additional instructions inside, as you think, the if block. But actually you will add instructions outside the block.

Ronin
  • 1,688
  • 1
  • 13
  • 23
  • But when I try: `if($_REQUEST['sub'] != 'expired') { $this->renderPartial('/layouts/navigation'); }` then error is `Undefined index: sub` – Dharmesh patel Mar 08 '16 at 08:44
  • @Dharmeshpatel To be honest, I cannot explain why, because I do not see context, do not see your code. You probably trying to get index without checking if the index exists. – Ronin Mar 08 '16 at 09:03