未定义的索引:升级MySql后的php yii中的XXXXX


Undefined index: XXXXX in php yii after upgrade MySql

我在Yii有一个网站。它工作得很好。但是,当我升级MySql时,我遇到了一些错误。

1.) date(): 依赖系统的时区设置是不安全的。

但是我已经通过定义时区解决了它。

2.) 未定义的索引:注册。

解决不了。那么,我该怎么办呢?我的代码如下:

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,
     ));
 }

在这里,如果我在我的 URL 中定义$_REQUEST['Registration']那么它将起作用,但我不能这样做,因为它在我的网站中无处不在。升级Mysql后发生错误。那么,我该怎么办?

谢谢

好的,

我不知道代码应该做什么,但我注意到的第一件事:

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

此部分缺少大括号。

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

会更有意义。否则,即使 isset 为假,它也会尝试设置注册。

你的错误在这里:

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

它与:

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

即使未设置,您也试图获得$_REQUEST['Registration']。因此,要修复它,请更改您的代码:

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

对于任何喜欢忽略大括号的人来说,当他们对if块有一个语句时,这通常是错误的。我强烈建议在任何情况下使用大括号,即使您if块下只有一个语句。

不正确的方法:

if (true)
    do_something();

正确的方法:

if (true) {
    do_something();
}

如果您使用不正确的方法,则在if块中添加其他指令时会遇到很多情况。但实际上你会在块外添加指令。