I wrote this code to upload a file and save the path in the database using the bind options
$model->attributes=$_POST['Form']; $uploadedFile=CUploadedFile::getInstance($model,'resume'); $path = '/protected/upload/'.time().$uploadedFile; if($model->validate()){ $connection=Yii::app()->db; $filePath=Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'.$path; // file upload location $result = $uploadedFile->saveAs($filePath); //upload file // an SQL with placeholders $hobbies=implode(",",$model->hobbies); // comma deliminated string of hobbies $dob =date('Ym-d',strtotime($model->dob)); // convert date $status=1; // status of record $sql='INSERT INTO student (name, dob, email, gender, hobbies, city, resume, message,status, created_date,modified_date) VALUES(:name,:dob,:email,:gender,:hobbies,:city,:resume,:msg,:status,now(),now())'; $command=$connection->createCommand($sql); // replace the placeholder with the actual username value $command->bindParam(":name",$model->name,PDO::PARAM_STR); $command->bindParam(":dob",$dob,PDO::PARAM_STR); $command->bindParam(":email",$model->email,PDO::PARAM_STR); $command->bindParam(":gender",$model->gender,PDO::PARAM_INT); $command->bindParam(":hobbies",$hobbies,PDO::PARAM_STR); $command->bindParam(":city",$model->city,PDO::PARAM_INT); $command->bindParam(":resume",$path,PDO::PARAM_STR); $command->bindParam(":msg",$model->msg,PDO::PARAM_STR); $command->bindParam(":status",$status,PDO::PARAM_INT); $result=$command->execute(); }
I hope this is helpful to you.