Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring information into database while image is stored in folder in Yii
    primarykey
    data
    text
    <p>What I want to do is to store information of users (id,name,photo). Id and name will be stored in database while photo is stored in images folder. I managed to store photos into folder but strangely the other information was not stored into database. What did I do wrong?please point me out! Tq Here is the code views/_form.php </p> <pre><code>&lt;?php /* @var $this PhotoController */ /* @var $model Photo */ /* @var $form CActiveForm */ ?&gt; &lt;div class="form"&gt; &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'photo-form', 'enableAjaxValidation'=&gt;false, 'htmlOptions'=&gt;array('enctype'=&gt;'multipart/form-data'), 'clientOptions'=&gt;array('validateOnSubmit'=&gt;true,), )); ?&gt; &lt;p class="note"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt; &lt;?php echo $form-&gt;errorSummary($model); ?&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'name'); ?&gt; &lt;?php echo $form-&gt;textField($model,'name',array('size'=&gt;32,'maxlength'=&gt;32)); ?&gt; &lt;?php echo $form-&gt;error($model,'name'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'foto'); ?&gt; &lt;?php echo $form-&gt;fileField($model,'foto'); ?&gt; &lt;?php echo $form-&gt;error($model,'foto'); ?&gt; &lt;/div&gt; &lt;div class="row buttons"&gt; &lt;?php echo CHtml::submitButton($model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt; &lt;/div&gt; &lt;?php $this-&gt;endWidget(); ?&gt; &lt;/div&gt;&lt;!-- form --&gt; </code></pre> <p>models/Photo.php</p> <pre><code>&lt;?php /** * This is the model class for table "tbl_photo". * * The followings are the available columns in table 'tbl_photo': * @property integer $id * @property string $name */ class Photo extends CActiveRecord{ public $foto; /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return Photo the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 'tbl_photo'; } /** * @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', 'required'), array('name', 'length', 'max'=&gt;32), array('foto', 'file', 'types'=&gt;'jpg, jpeg, gif, png'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, name', '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', ); } /** * 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. $criteria=new CDbCriteria; $criteria-&gt;compare('id',$this-&gt;id); $criteria-&gt;compare('name',$this-&gt;name,true); return new CActiveDataProvider($this, array( 'criteria'=&gt;$criteria, )); } } </code></pre> <p>controllers/PhotoController.php</p> <pre><code>&lt;?php class PhotoController extends Controller{ ... ... /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate(){ $model=new Photo; // Uncomment the following line if AJAX validation is needed // $this-&gt;performAjaxValidation($model); if(isset($_POST['Photo'])){ $model-&gt;attributes=$_POST['Photo']; if(isset($_FILES) &amp;&amp; $_FILES['Photo']['error']['foto']==0){ $uploadedFile = CUploadedFile::getInstance($model, 'foto'); if($uploadedFile-&gt;getExtensionName()=="jpeg" || $uf-&gt;getExtensionName()=="png" || $uf-&gt;getExtensionName()=="gif"){ $uploadedFile-&gt;saveAs(Yii::getPathOfAlias('webroot').'/images/'.$uploadedFile-&gt;getName()); Yii::app()-&gt;user-&gt;setFlash('foto','/images/'.$uploadedFile-&gt;getName()); $this-&gt;refresh(); } } if($model-&gt;save())$this-&gt;redirect(array('view','id'=&gt;$model-&gt;id)); } $this-&gt;render('create',array('model'=&gt;$model,)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload