Yii更新案例显示空屏幕

时间:2014-03-04 21:49:36

标签: php forms yii

我有一个用于编辑用户记录的表单。它首次更新记录,但是当我再次按下更新按钮时,它会显示一个空白屏幕并且它也会取消设置。我无法弄清楚我做错了什么 以下是我的代码

public function actionEditProfile($affusername = NULL) {

    $this->layout = 'layout_home';

    if ($this->VarifyUser($affusername) && Yii::app()->user->id) {

        $model = new Users('edit'); //apply rules if user comes directly to signup page
        // uncomment the following code to enable ajax-based validation
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'editprofile-form') {
            CActiveForm::validate($model);
            Yii::app()->end();
        }

        if (isset($_POST['Users'])) {


            $the_image = CUploadedFile::getInstance($model, 'image');
            if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {

                $model->image = $the_image;
            }


            if (is_object($the_image)) {

                $file_name = Yii::app()->user->id . '.' . $model->image->extensionName;

                $move = $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/myproject/images/users/' . $file_name);

                $_POST['Users']['image'] = $file_name;
            }


            $model->attributes = $_POST['Users'];


            if ($model->validate()) {

                // form inputs are valid, do something here
                if ($model->updateByPk(Yii::app()->user->id, $_POST['Users'])) {
                    //$this->setFlashSuccess("Unable to register user, please try again");
                    $this->setFlashSuccess('Updated successfully, now redirecting.');
                    //$this->redirect($this->base_url);
                } else {
                    $this->setFlashError("Unable to update user, please try again");
                    return false;
                }
            } else {
                print_r($model->getErrors());
                echo 'validation  fail';
                exit;
            }
        }

        $user_data = Users::model()->getUsersByUserName($affusername); //getting user information
        //print_r($user_data);
        //$model=new Users('nonfb-create'); //apply rules if user comes directly to signup page
        $this->render('editprofile', array('model' => $model, 'data' => $user_data,));
    } else {
        $this->redirect($this->base_url);
    }
}

我的观点是

<div class="adminform_wrapp">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'editprofile-form',
        'enableAjaxValidation' => false,
        'clientOptions' => array(
            'validateOnSubmit' => true,
        ),
        'enableClientValidation' => true,
        'focus' => array($model, 'first_name'),
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data'
        )
    ));
    echo $form->errorSummary($model);
    ?>

    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_First_Name">First Name: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'first_name', array('value' => $data['first_name'])); ?>
        <?php $form->error($model, 'first_name'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_Last_Name">Last Name: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'last_name', array('value' => $data['last_name'])); ?>
        <?php $form->error($model, 'last_name'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_Email">Email: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'email', array('value' => $data['email'])); ?>
        <?php $form->error($model, 'email'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_Phone_No">Phone No: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'phone_no', array('value' => $data['phone_no'])); ?>
        <?php $form->error($model, 'phone_no'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label>Username:</label>
        <span class="profile-username"><?php echo $data['username']; ?></span> </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label>Edit Profile picture:</label>
        <span class="image-placeholder">
            <img src="<?php echo $this->theme_baseurl . '/images/users/' . $data['image']; ?>" style="width:96px; height:96px;"/>
        </span>
        <div id='file_browse_wrapper'>

            <?php
            //echo $form->labelEx($model, 'image');
            echo $form->fileField($model, 'image', array('id' => 'file_browse'));
            echo $form->error($model, 'image');
            ?>
        </div>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <?php echo $form->labelEx($model, 'Address', array('class' => 'fieldname')); ?>
        <?php echo $form->textField($model, 'address', array('value' => $data['address'])); ?>
        <?php $form->error($model, 'address'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_Country">Country: <span class="required">*</span></label>
        <select class="error" onchange="print_state('Users_state', this.selectedIndex);" id="Users_country" name ="Users[country]"></select>
        <?php $form->error($model, 'country'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_State">State: <span class="required">*</span></label>
        <select name ="Users[state]" id ="Users_state"></select>
        <script language="javascript">print_state("Users_state", '', "<?php echo $data['state'] ?>");</script>
        <?php $form->error($model, 'state'); ?>
    </div>
    <!--adminform_row-->
    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_City">City: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'city', array('value' => $data['city'])); ?>
        <?php $form->error($model, 'city'); ?>
    </div>
    <!--adminform_row-->

    <!--adminform_row-->
    <div class="adminform_row">
        <label class="fieldname required" for="Users_Zipcode">Zipcode: <span class="required">*</span></label>
        <?php echo $form->textField($model, 'zipcode', array('value' => $data['zipcode'])); ?>
        <?php $form->error($model, 'zipcode'); ?>
    </div>
    <!--adminform_row-->

    <!--adminform_row-->
    <div class="adminform_row">
        <input type="submit" class="adminupdate_btn" value="Update">
        <input type="reset" class="admincancel_btn" value="Cancel">
    </div>
    <!--adminform_row-->

    <?php $this->endWidget(); ?>
</div>

2 个答案:

答案 0 :(得分:0)

当出现语法错误时,通常会显示空白屏幕。 您可以在apache日志中看到错误(通常在linux中的/var/log/apache2/error.log中)。 正如darkheir所说,您应该看到该日志文件。

答案 1 :(得分:0)

这是语法错误。它还更新了数据库中的完整行,其中更新表单只发送了几个字段。我通过手动设置属性来修复。以下是我的代码

 if (isset($_POST['Users'])) {
            //echo "<pre>";
            //print_r($model->image);
            // print_r($_POST);
            // print_r($_FILES);
            //echo "</pre>";

            $the_image = CUploadedFile::getInstance($model, 'image');

            if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {

                $model->image = $the_image;
            }
            //echo "<pre>";
            //print_r($_POST);
            //echo "</pre>";
            $first_name = $_POST['Users']['first_name'];
            $last_name = $_POST['Users']['last_name'];
            $email = $_POST['Users']['email'];
            $phone_no = $_POST['Users']['phone_no'];
            $address = $_POST['Users']['address'];
            $country = $_POST['Users']['country'];
            $state = $_POST['Users']['state'];
            $city = $_POST['Users']['city'];
            $zipcode = $_POST['Users']['zipcode'];
            if (is_object($the_image)) {

                $file_name = Yii::app()->user->id . '.' . $model->image->extensionName;

                $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/karmora/images/users/' . $file_name);

                $_POST['Users']['image'] = $file_name;
                //$model->first_name  =   $_POST['Users']['first_name'];
                $attributes = array(
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'email' => $email,
                    'phone_no' => $phone_no,
                    'image' => $file_name,
                    'address' => $address,
                    'country' => $country,
                    'state' => $state,
                    'city' => $city,
                    'zipcode' => $zipcode
                );
            } else {
                $attributes = array(
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'email' => $email,
                    'phone_no' => $phone_no,
                    'address' => $address,
                    'country' => $country,
                    'state' => $state,
                    'city' => $city,
                    'zipcode' => $zipcode);
            }


            $model->updateByPk(Yii::app()->user->id, $attributes);
        }