php - If I do not update any information in my update form and click "Save" button,the valu of my database auto increase -
i in learning stage in php.please me update form.advanced thanks. have form update user information button "save" , "reset" button.i update information version number increase.but problem when not update information , click on "save" button version number increases.it should not increase.i giving controller , form code....
public function actioncreate() { $model=new cvupload; $model2=new cvupload; $usertype=new usertype; $cvhistory=new cvhistory; $this->performajaxvalidation($model); if(!empty($_post)) { $id = $_post['cvupload']['user_id']; $cv_type = $_post['cvupload']['cv_type']; $criteria = new cdbcriteria(); $criteria->condition = "user_id = $id , is_current='yes' , cv_type='$cv_type'"; $cvuploadmodel = cvupload::model()->find($criteria); if(!empty($cvuploadmodel)) { $cvuploadmodel->is_current='no'; $cvuploadmodel->status='inactive'; if($cvuploadmodel->save(false)){ $model->attributes=$_post['cvupload']; $model->upload_date = new cdbexpression("now()"); $model->update_date = new cdbexpression("now()"); $model->version_id = $cvuploadmodel->version_id+1; if($model->save(false)) { $cvhistory->cv_id = $model->cv_id; $cvhistory->user_id = $model->user_id; $cvhistory->job_title_id = $model->job_title_id; $cvhistory->cv_type = $model->cv_type; $cvhistory->version_id = $model->version_id; $cvhistory->file_name = $model->file_name; $cvhistory->upload_date = $model->upload_date; $cvhistory->update_date = $model->update_date; $cvhistory->is_current = $model->is_current; $cvhistory->status = $model->status; if($cvhistory->save(false)) { $this->redirect(array('view','id'=>$model->cv_id)); } } } } else { $model->attributes=$_post['cvupload']; $model->upload_date = new cdbexpression("now()"); $model->update_date = new cdbexpression("now()"); $model->version_id=1; if($model->save()) { $cvhistory->cv_id = $model->cv_id; $cvhistory->user_id = $model->user_id; $cvhistory->job_title_id = $model->job_title_id; $cvhistory->cv_type = $model->cv_type; $cvhistory->version_id = $model->version_id; $cvhistory->file_name = $model->file_name; $cvhistory->upload_date = $model->upload_date; $cvhistory->update_date = $model->update_date; $cvhistory->is_current = $model->is_current; $cvhistory->status = $model->status; if($cvhistory->save(false)) { $this->redirect(array('view','id'=>$model->cv_id)); } } } } $this->render('create',array( 'model'=>$model, 'usertype'=>$usertype, 'cvhistory'=>$cvhistory, )); } public function actionupdate($id) { $model=$this->loadmodel($id); $cvhistory=new cvhistory; if(isset($_post['cvupload'])) { $model->attributes=$_post['cvupload']; if($model->save()) { $criteria = new cdbcriteria(); $criteria->condition = "cv_id = $model->cv_id , is_current = 'yes'"; $cvhistory = cvhistory::model()->find($criteria); $cvhistory->is_current = 'no'; $cvhistory->status = 'inactive'; if($cvhistory->save(false)) { $new_cvhistory=new cvhistory; $new_cvhistory->cv_id = $model->cv_id; $new_cvhistory->user_id = $model->user_id; $new_cvhistory->job_title_id = $model->job_title_id; $new_cvhistory->cv_type = $model->cv_type; $new_cvhistory->version_id = $cvhistory->version_id+1; $model->version_id = $new_cvhistory->version_id ; $new_cvhistory->file_name = $model->file_name; $new_cvhistory->upload_date = $model->upload_date; $new_cvhistory->update_date = new cdbexpression("now()"); $model->update_date = $new_cvhistory->update_date ; $new_cvhistory->status = $model->status; $new_cvhistory->is_current = "yes"; $new_cvhistory->save(false); $model->save(false); if($new_cvhistory->save(false))$this->redirect(array('view','id'=>$model->cv_id)); } } } $this->render('update',array( 'model'=>$model, 'cvhistory'=>$cvhistory, )); }
my form button code:
<style> #type { background-color:#f5f5f5; font-family: 'arial'; font-size: 20px; text-decoration: none; color:#b2b2b2; border:none; float: right; } div.form label { display: block; font-size: 1em; font-weight: bold; } .types-add{ float: right; } #type:hover { border: none; } </style> <?php if (yii::app()->user->hasflash('created')): ?> <div class="flash-success"> <?php echo yii::app()->user->getflash('created'); ?> </div> <?php endif; ?> <div class="form"> <?php $form=$this->beginwidget( 'booster.widgets.tbactiveform', array( 'id'=>'cvupload-form', 'type' => 'horizontal', 'htmloptions' => array( 'class' => 'well', 'enctype' => 'multipart/form-data', ) )); ?> <fieldset> <h1 align="center">cv upload</h1> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary($model); ?> <div class="row"> <?php $criteria=new cdbcriteria(); echo $form->dropdownlistgroup( $model, 'user_id', array( 'wrapperhtmloptions' => array( 'class' => 'col-sm-5', ), 'widgetoptions' => array( 'data' => chtml::listdata(user::model()->findall($criteria), 'id', 'user_id'), 'dataprovider'=>$model->searchbyuserid(yii::app()->user->getid()), 'htmloptions' => array('prompt'=>'select'), ) ) ); ?> </div> <div class="row" id="jobtitle"> <?php $criteria = new cdbcriteria; $criteria->condition = "status= 'active'"; echo $form->dropdownlistgroup( $model, 'job_title_id', array( 'wrapperhtmloptions' => array( 'class' => 'col-sm-5', ), 'widgetoptions' => array( 'data' => chtml::listdata(jobtitle::model()->findall($criteria), 'id', 'name'), 'htmloptions' => array('prompt'=>'select job title'), ) ) ); ?> </div> <?php echo chtml::form($this->createurl('uploadreport'), 'post', array('enctype'=>'multipart/form-data'));?> <label class="col-sm-3 control-label required" for="cvupload_file_name"> upload file </label> <div class="row" id="file_upload" style="margin-left:20%; margin-bottom:2%;"> <?php $this->widget('cmultifileupload',array( 'name' => 'files', 'accept' => 'doc|docx|pdf', 'max' => 1, 'htmloptions' => array('size' => 25), )); echo chtml::htmlbutton('upload',array( 'onclick'=>'javascript: send();', // on submit call js send() function 'id'=> 'post-submit-btn', // button id 'newcreate' 'class'=>'btn btn-primary', )); ?> </div> <?php yii::app()->clientscript->registercorescript("jquery"); ?> <script src="http://code.jquery.com/jquery-migrate-1.2.0.js"></script> <script> function send(){ var formdata = new formdata($("#cvupload-form")[0]); $.ajax({ url: '<?php echo yii::app()->createurl("cvupload/upload"); ?>', type: 'post', data: formdata, datatype:'json', beforesend: function() { }, success: function (data) { $("#cvupload_file_name").val(data); }, complete: function() { }, error: function (data) { alert("there may error on uploading. try again later"); }, cache: false, contenttype: false, processdata: false }); return false; } </script> <div class="row" id="file_name"> <?php echo $form->textfieldgroup( $model,'file_name', array( 'wrapperhtmloptions' => array( 'class'=> 'col-sm-5', ), ) ); ?> </div> <div class="row" id="statustype"> <?php $status = array('active'=>'active', 'inactive'=>'inactive'); echo $form->dropdownlistgroup( $model, 'status', array( 'wrapperhtmloptions' => array( 'class' => 'col-sm-5', ), 'widgetoptions' => array( 'data' => $status, 'htmloptions' => array('prompt'=>'select status'), ) ) ); ?> </div> <div class="row" id="statustype"> <?php $is_current = array('yes'=>'yes', 'no'=>'no'); echo $form->dropdownlistgroup( $model, 'is_current', array( 'wrapperhtmloptions' => array( 'class' => 'col-sm-5', ), 'widgetoptions' => array( 'data' => $is_current, 'htmloptions' => array('prompt'=>'select status'), ) ) ); ?> </div> <div class="row buttons"> <?php echo chtml::submitbutton($model->isnewrecord ? 'create' : 'save',array( 'class'=>'btn btn-primary', 'style'=>"position:relative; margin-left:25%", 'id'=> 'yt1', // button id )); ?> </div> </fieldset> <?php $this->endwidget(); ?> </div><!-- form --> <!--/*/* <div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'cv-upload-form', 'enableajaxvalidation'=>false, )); ?> <p class="note">fields <span class="required">*</span> required.</p> <?php echo $form->errorsummary($model); ?> <div class="row"> <?php echo $form->labelex($model,'user_id'); ?> <?php echo $form->textfield($model,'user_id',array('size'=>20,'maxlength'=>20)); ?> <?php echo $form->error($model,'user_id'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'upload_date');enter code here ?> <?php echo $form->textfield($model,'upload_date'); ?> <?php echo $form->error($model,'upload_date'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'file_name'); ?> <?php echo $form->textfield($model,'file_name',array('size'=>60,'maxlength'=>150)); ?> <?php echo $form->error($model,'file_name'); ?> </div> <div class="row"> <?php echo $form->labelex($model,'file_location'); ?> <?php echo $form->textfield($model,'file_location',array('size'=>60,'maxlength'=>150)); ?> <?php echo $form->error($model,'file_location'); ?> </div> <div class="row buttons"> <?php echo chtml::submitbutton($model->isnewrecord ? 'create' : 'save'); ?> </div> <?php $this->endwidget(); ?> </div><!-- form -->
if don't change form values, doesn't mean data wouldn't sended server.
here @ least button save sended, case true:
if(!empty($_post))
you didn't show whole form, suggest fields user_id , cv_type has values (no mater values changed or not, if form has fields submited)
$id = $_post['cvupload']['user_id']; $cv_type = $_post['cvupload']['cv_type'];
that's why model $cvuploadmodel = cvupload::model()->find($criteria);
loads , increament version_id
Comments
Post a Comment