Confusion in inserting data

If i want to insert data entry in a table from 2 tables.

Table 1-StudentCourseRelation

Table 2-Course

Table 3-User

I want to display contents from table 2 and table 3 in table 1.It shows data but if student registered already in Table-1,data should not be entered again.But in my code,it is registering same course multiple times.

My code is given below:

 public function actionRegister() 




             echo $id;


             echo $login_id;

             $model1=new StudentCourseRelation;

             $model_login=  StudentCourseRelation::model()->findAll(array('condition'=>"user_id='$login_id'"));

              $model_course=  StudentCourseRelation::model()->findAll(array('condition'=>"course_id='$id'"));




              if(empty($model_course->course_id)&& empty($model_login->user_id))





                    echo "successfully registered...";









                      echo 'Already kiya hai!!';







                    echo "successfully registered kela ha";



 else {




                    echo "successfully registered..!!";









         <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/styles.css" />




            <?php $form=$this->beginWidget('CActiveForm', array(



                    'clientOptions' => array(


            )); ?>

            <div class="coursediv">



                <table class="ctable">


                        <td class="coursetd">Select Course</td>

                        <td><div class="dropdown">


                    echo $form->errorSummary($model);                 


                    echo CHtml::activeDropDownList($model,'course_name',$type_list,array('empty'=>'Select Course',

                        'onChange'=> Chtml::ajax(array(


                        'url'=>  CController::createUrl('Getdata'),



                        'success' => 'function(data)


                                var obj = JSON.parse(data);

                                var detail = obj.course_details; 

                                var session= obj.session;





              <?php echo $form->error($model,'course_name'); ?>





                        <td class="coursetd">Course Details:</td>

                        <td><p id="drop2" class="details"></p></td>



                        <td class="coursetd">Course Duration:</td>

                        <td><p id="drop3" class="course"></p></td>






                <?php echo CHtml::link('Register',array('Register',)); ?>

<!-- result goes in this div-->

<div id="save_result"></div>


            </div><!-- form -->

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



My dropdown is working properly.

I’m confused at what you’re trying to do.


Why are you echoing items in your function…this does nothing and you should remove them.

You said you want it in a table…like cgridview?

You will need to show the action for the view because it’s not action register.

You will need to show the action getData (used in your ajax for the drop down).

Why are you using an ajax drop down? It doesn’t seem necessary but i could be missing something.

I would assume you are trying to show a table of courses the user is registered in and also be able to add courses to it…is this correct?

You can create a custom validation rule in your studencourserelation model to prevent from registering more than once for a course.

public function checkIfRegistered($attribute, $params) {

    	$studenCourseModel = StudentCourseRelation::model()->find(array(

        	'condition' => 'user_id=:user_id && course_id=:course_id',

        	'params' => array(':user_id' => $this->user_id, ':course_id' => $this->course_id),

        	'limit' => 1


    	if (!empty($studenCourseModel) && isset($studenCourseModel)) {

        	$this->addError($attribute, 'You are already registered for this course!');



	public function rules() {

    	return array(

        	array('course_id', 'checkIfRegistered'),



also you are doing two queries to find the same thing

$model_login=  StudentCourseRelation::model()->findAll(array('condition'=>"user_id='$login_id'"));

$model_course=  StudentCourseRelation::model()->findAll(array('condition'=>"course_id='$id'"));


you can make it one like this

$studenCourseModel = StudentCourseRelation::model()->findall(array(

        	'condition' => 'user_id=:user_id && course_id=:course_id',

        	'params' => array(':user_id' => $login_id, ':course_id' => $id)


You should also do checks to make sure the session values exist or your program could throw errors for users. Why are you storing them in sessions as well? I feel this could be greatly reduced if you explained what you are trying to do better.

Thanks I find the solution .I make unique Course_id and user_id.and i am using Try catch to catch the error when user register the same course.again thanks a lot you advice help in reducing the line of codes.

Actually i am new in php and yii as well so i do some silly mistakes.