php - 如何解决这个错误 "Undefined variable: id"

标签 php mysql yii

我是 Yii 新手。如何解决“ undefined variable :id”错误?

$adminid = adminuser::model()->findByPK($id)->id;
p($adminid);
echo $agent->id;

adminuser.php 代码:-

<?php

/**
 * This is the model class for table "adminuser".
 *
 * The followings are the available columns in table 'adminuser':
 * @property integer $id
 * @property string $Name
 * @property string $Email
 * @property string $Password
 * @property string $DateRegistered
 * @property integer $PermissionsLevel
 * @property integer $Active
 *
 * The followings are the available model relations:
 * @property Contactus[] $contactuses
 * @property Contactus[] $contactuses1
 */
class Adminuser extends CActiveRecord
{
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'adminuser';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('Email, PermissionsLevel', 'required'),
        array('PermissionsLevel, Active', 'numerical', 'integerOnly'=>true),
        array('Name, Email, Password', 'length', 'max'=>100),
        array('DateRegistered', 'safe'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, Name, Email, Password, DateRegistered, PermissionsLevel, Active', 'safe', 'on'=>'search'),
    );
}

/**
 * @return array relational rules.
 */
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'contactuses' => array(self::HAS_MANY, 'Contactus', 'AdminEmailID'),
        'contactuses1' => array(self::HAS_MANY, 'Contactus', 'AdminID'),
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'Name' => 'Name',
        'Email' => 'Email',
        'Password' => 'Password',
        'DateRegistered' => 'Date Registered',
        'PermissionsLevel' => 'Permissions Level',
        'Active' => 'Active',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('Name',$this->Name,true);
    $criteria->compare('Email',$this->Email,true);
    $criteria->compare('Password',$this->Password,true);
    $criteria->compare('DateRegistered',$this->DateRegistered,true);
    $criteria->compare('PermissionsLevel',$this->PermissionsLevel);
    $criteria->compare('Active',$this->Active);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Adminuser the static model class
 */
public static function model($className=__CLASS__)
{
    return parent::model($className);
}

}

最佳答案

只需在模型的 findByPk() 之前使用 isset() 检查您的 $id 变量即可。

if(isset($id) && $id>0)
{
  $adminid = adminuser::model()->findByPk($id);
  #check what it returns 
  echo '<pre>';
  print_r($adminid); 
  echo '</pre>';
 #then do your further process
 }else{
  echo "id is not exists";
}

关于php - 如何解决这个错误 "Undefined variable: id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152026/

相关文章:

mysql - 在 SQL 中选择多个变量

php - Yii authManager 业务规则写在哪里?

PHP 数组索引与数组关联

php - 使用 Y-m-dTH :i:s 验证 Laravel 日期格式

php - 在另一个页面调用一个页面会导致网站变慢吗?

MySQL 查询和连接

php - 如何使用命令行访问linux服务器上的php.ini

php - 使用 PDO 多次 INSERT 到 3 个表中

yii - 如何让文件上传按钮显示在 EditMe 扩展中

yii - 如何使用 CActiveRecord 方法将数据库字段更新为其自身的函数而不实例化模型?