unable to save data in database

Hello! I am newbee in this platform. I created a form using form genrator but unable to insert the data into the mysql database. Please help me.

here is my controller action

public function actionCareer()

{

$model=new Career;








if(isset($_POST['ajax']) && $_POST['ajax']==='career-career-form')


{


    echo CActiveForm::validate($model);


    Yii::app()->end();


}








if(isset($_POST['Career']))


{


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


    if($model->validate())


    {


        Yii::app()->user->setFlash('career','Thank you for contacting us. We will respond to you as soon as possible.');


			$this->refresh();


        return;


    }


}


$this->render('career',array('model'=>$model));

}

Thanks,

Rohit

Ohh …Rohit where is the model class method $model->save() to insert a data into a database. :blink:

/* moved to General Discussion */

can u explain jayant :)

CActiveRecord::save()

can any1 help me:

here’s my modal :

<?php

/**

  • This is the model class for table "tbl_career".

  • The followings are the available columns in table ‘tbl_career’:

  • @property integer $id

  • @property string $Name

  • @property string $City

  • @property string $Email

  • @property integer $Phone

  • @property string $Qualification

  • @property string $Skills

  • @property string $Resume

*/

class Career extends CActiveRecord

{

public $Name;

public $City;

public &#036;Email;


public &#036;Phone;


public &#036;Qualification;


public &#036;Skills;


public &#036;Resume;





/**


 * Returns the static model of the specified AR class.


 * @return Career the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'tbl_career';


}





/**


 * @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('Name, City, Email, Phone, Qualification, Skills, Resume', 'required'),


		array('Phone', 'numerical', 'integerOnly'=&gt;true),


		array('Name', 'length', 'max'=&gt;50),


		array('City', 'length', 'max'=&gt;20),


		array('Email', 'length', 'max'=&gt;30),


		array('Resume', 'length', 'max'=&gt;100),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, Name, City, Email, Phone, Qualification, Skills, Resume', 'safe', 'on'=&gt;'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(


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'Name' =&gt; 'Name',


		'City' =&gt; 'City',


		'Email' =&gt; 'Email',


		'Phone' =&gt; 'Phone',


		'Qualification' =&gt; 'Qualification',


		'Skills' =&gt; 'Skills',


		'Resume' =&gt; 'Resume',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('Name',&#036;this-&gt;Name,true);


	&#036;criteria-&gt;compare('City',&#036;this-&gt;City,true);


	&#036;criteria-&gt;compare('Email',&#036;this-&gt;Email,true);


	&#036;criteria-&gt;compare('Phone',&#036;this-&gt;Phone);


	&#036;criteria-&gt;compare('Qualification',&#036;this-&gt;Qualification,true);


	&#036;criteria-&gt;compare('Skills',&#036;this-&gt;Skills,true);


	&#036;criteria-&gt;compare('Resume',&#036;this-&gt;Resume,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}

public function save($runValidation=false,$attributes=null)

{

if(&#33;&#036;runValidation || &#036;this-&gt;validate(&#036;attributes))


    return &#036;this-&gt;getIsNewRecord() ? &#036;this-&gt;insert(&#036;attributes) : &#036;this-&gt;update(&#036;attributes);


else


    return false;

}

}

controller : :

// Displays the Career page

public function actionCareer()

{

&#036;model=new Career;








if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='career-career-form')


{


    echo CActiveForm::validate(&#036;model);


    Yii::app()-&gt;end();


}








if(isset(&#036;_POST['Career']))


{


    &#036;model-&gt;attributes=&#036;_POST['Career'];


    if(&#036;model-&gt;validate())


    {


                


                    &#036;career=new career;                  


                    &#036;career-&gt;id = &quot;&quot;;


                    &#036;career-&gt;name=&quot;&quot;;                    


                    &#036;career-&gt;city=&quot;&quot;;


                    &#036;career-&gt;email = &quot;&quot;;


                    &#036;career-&gt;phone = &quot;&quot;;


                    &#036;career-&gt;qualifications = &quot;&quot;;           


                    &#036;career-&gt;skills = &quot;&quot;;


		&#036;career-&gt;resume = &quot;&quot;;


                    if (&#036;career-&gt;validate()){


                    &#036;model-&gt;save(false);


                    &#036;career-&gt;save(); //this does not save into db and return false


                    }


                    /* My hack method that works


                    &#036;sql = 'INSERT INTO {{profiles}} (id,u_id,lastname,firstname,date_of_birth,about) VALUES (&quot;&quot;,:u_id,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;)';


                    &#036;parameters = array(&quot;:u_id&quot;=&gt;&#036;model-&gt;id);


                    Yii::app()-&gt;db-&gt;createCommand(&#036;sql)-&gt;execute(&#036;parameters);


                    */                      


     


              &#036;this-&gt;redirect(Yii::app()-&gt;homeUrl);





    }


}


&#036;this-&gt;render('career',array('model'=&gt;&#036;model));

}

view : :

<div class="form">

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

'id'=&gt;'career-career-form',


'enableAjaxValidation'=&gt;true,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Name'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Name'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Name'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'City'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'City'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'City'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Email'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Email'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Email'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Phone'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Phone'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Phone'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Qualification'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Qualification'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Qualification'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Skills'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Skills'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Skills'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Resume'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Resume'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'Resume'); ?&gt;


&lt;/div&gt;








&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Submit'); ?&gt;


&lt;/div&gt;

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

/* No need to create a new topic. Topics merged */

For better readability use code tags when posting code. Click this symbol in the editor .

First, you should start by reading The Definitive Guide to Yii thoroughly. In the created CRUD code you will find the answer you need.

/Tommy


/* No need to create a new topic. Topics merged */


For better readability use code tags when posting code. Click this symbol in the editor Posted Image.


First, you should start by reading The Definitive Guide to Yii thoroughly. In the created CRUD code you will find the answer you need.


/Tommy 

this is custom form … i didn’t use crud

You don’t need save() method in your AR class… it’s already implemented in base CActiveRecord.

Please check if you’re in YII_DEBUG mode and php.ini configuration allows error displaying.

That way you’ll be able to see if there are some problems with database.




if(isset($_POST['Career']))

{

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

if($model->validate())

{


$career=new career; 

$career->id = ""; // maybe this is a problem... check your table definition

$career->name=""



Think that problem here is with validate() and not save().

So please be sure that you entered the block after $career->validate()

if save is returning false, dump $career->errors after it fails to save.