controller
<?php namespace frontend\controllers; use Yii; use yii\web\Controller; use frontend\models\Zou2; use yii\web\UploadedFile; class Zou2Controller extends Controller{ public $enableCsrfValidation=false; //添加 public function actionIndex(){ //实例化 $model = new Zou2(); if(Yii::$app->request->isPost){ $arr['name']=Yii::$app->request->post('Zou2')['name']; //可以一起全部接过来 $arr['pwd']=Yii::$app->request->post('Zou2')['pwd']; $arr['sex']=Yii::$app->request->post('Zou2')['sex']; $arr['tel']=Yii::$app->request->post('Zou2')['tel']; $arr['email']=Yii::$app->request->post('Zou2')['email']; $arr['content']=Yii::$app->request->post('Zou2')['content']; $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); //print_r($model->imageFile);die; //$a=json_encode($model->imageFile); //$b=json_decode($a,true); //$res=Yii::$app->db->createCommand()->insert('uploads', ['img' =>'uploads/'.$b['name']])->execute(); //print_r($b);die; if ($model->upload()) { $a=json_encode($model->imageFile); $b=json_decode($a,true); $arr['imageFile']='uploads/'.$b['name']; //$res=Yii::$app->db->createCommand()->insert('uploads', ['img' =>'uploads/'.$b['name']])->execute(); $arr=Yii::$app->db->createCommand()->insert('zou2',$arr)->execute(); }else{ echo "失败"; } /* $arr=Yii::$app->db->createCommand()->insert('zou2',$arr)->execute(); if($arr){ echo "<script>alert('添加成功');location.href='index.php?r=zou2/lists'</script>"; }*/ }else{ return $this->render('index',['model'=>$model]); } } //显示 public function actionLists(){ $arr['res'] = Yii::$app->db->createCommand('select * from zou2')->queryAll(); return $this->render('list',$arr); } //修改 public function actionUpdate(){ $model = new Zou2(); $id=Yii::$app->request->get('id'); $sql="select * from zou2 where id=$id"; $res = Yii::$app->db->createCommand($sql)->queryOne(); return $this->render('update',['model'=>$model,'res'=>$res]); } //接收修改数据 public function actionUpdates(){ //实例化 $model = new Zou2(); if(Yii::$app->request->isPost){ $arr=Yii::$app->request->post(); $brr=$arr['Zou2']; $id=$brr['id']; unset($brr['id']); //print_r($brr);exit; $res=Yii::$app->db->createCommand()->update('zou2',$brr,'id='.$id)->execute(); if($res){ echo "<script>alert('修改成功');location.href='index.php?r=zou2/lists'</script>"; } }else{ return $this->render('index',['model'=>$model]); } } //删除 public function actionDel(){ $id=Yii::$app->request->get('id'); $sql="delete from zou2 where id=$id"; $arr=Yii::$app->db->createCommand($sql)->execute(); if($arr){ $this->redirect('index.php?r=zou2/lists'); }else{ $this->redirect('index.php?r=zou2/lists'); } } } model<?php namespace frontend\models; use Yii; use yii\base\Model; use yii\web\UploadedFile; /* * This is the model class for table "zou2". * * @property integer $id * @property string $email * @property string $pwd * @property string $repwd * @property string $name * @property string $sex * @property string $address */ class Zou2 extends \yii\db\ActiveRecord { public $code; public $repwd; public $content; public $id; public $imageFile; public static function tableName() { return 'zou2'; } /* * @inheritdoc */ public function rules() { return [ [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg'], [['email','tel', 'pwd', 'repwd', 'name', 'sex'], 'string', 'max' => 255], [['email','tel', 'pwd', 'repwd', 'name', 'sex','code','content'], 'required','message'=>'不能为空'], ['name','match','pattern'=>'/^[a-zA-Z][a-zA-Z0-9]{4,9}$/','message'=>'昵称必须是5-10位字母、数字组成,且不能以数字开头'], ['pwd','match','pattern'=>'/^[a-zA-Z0-9_]{4,10}$/','message'=>'密码必须是5-10位字母、数字、下划线'], ['repwd',"compare","compareAttribute"=>"pwd","message"=>"两次密码不一致"], ['tel','match','pattern'=>'/^1[34578]\d{9}$/','message'=>'手机号格式不正确'], ['email','email','message'=>'输入正确邮箱格式'], ['content','match','pattern'=>'/^[\u4e00-\u9fa5]{1,20}$/','message'=>'简介必须是汉字,且不能超过20个汉字,'], ['code', 'captcha','message'=>'验证码不正确'], ]; } public function upload(){ //由于图片上传时和表单验证有些冲突,因此把$this->validate();删了;会解决这个问题的可以告诉我下啊,(这个YII还是有些生疏啊) //$this->validate(); //print_r($this->errors()) ;die; $save=$this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension); if($save){ return true; }else{ return false; } /*if ($this->validate()) { $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension); //return true; } else { return false; }*/ } /* * @inheritdoc */ public function attributeLabels() { return [ 'name' => '昵称', 'id' => '', 'pwd' => '密码', 'repwd' => '再次输入密码', 'sex' => '性别', 'imageFile' => '头像', 'email' => '邮箱', 'tel' => '手机号', 'content' => '简介', 'code' => '验证码', ]; } }view index
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\captcha\Captcha; $form = ActiveForm::begin([ 'action' => 'index.php?r=zou2/index', 'method' => 'post', 'options' => ['enctype' => 'multipart/form-data'] ]) ?> <table align="center"> <tr> <td colspan="2"><a href="index.php?r=zou2/lists">显示页面</a></td> </tr> <tr><td colspan="2"><?= $form->field($model, 'name')->textInput(['style'=>'width:250px']) ?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'pwd')->passwordInput(['style'=>'width:250px'])?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'repwd')->passwordInput(['style'=>'width:250px'])?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'sex')->radioList(array(1=>'男',0=>'女'))?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'imageFile')->fileInput()?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'tel')->textInput(['style'=>'width:250px']) ?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'email')->textInput(['style'=>'width:250px']) ?></td></tr> <tr><td colspan="2"><? echo $form->field($model, 'content')->textarea(['rows'=>4]) ?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'code') ->widget( Captcha::className(), ['template' => '<div class="row"> <div class="col-lg-4">{input}</div> <div class="col-lg-6">{image}</div> </div>']) ?></td></tr> <tr><td colspan="2"><div class="form-group"> <div class="col-lg-offset-1 col-lg-11"> <?= Html::submitButton('注册', ['class' => 'btn btn-primary']) ?> </div> </div></td></tr> </table> <?php ActiveForm::end() ?>list
<p align="center"> <a href="index.php?r=zou2/index">添加页面</a> </p> <table class="table table-striped"> <tr> <th>ID</th> <th>昵称</th> <!-- <th>密码</th>--> <th>性别</th> <th>手机号</th> <th>邮箱</th> <th>简介</th> <th>操作</th> </tr> <?php foreach ($res as $v): ?> <tr> <td><?php echo $v['id']?></td> <td><?php echo $v['name']?></td> <td> <?php if($v['sex']==1){ echo "男"; }else{ echo "女"; } ?> </td> <td><?php echo $v['tel']?></td> <td><?php echo $v['email']?></td> <td><?php echo $v['content']?></td> <td> <a href="index.php?r=zou2/update&id=<?php echo $v['id'] ?>">修改</a>|| <a href="index.php?r=zou2/del&id=<?php echo $v['id']?>">删除</a> </td> </tr> <?php endforeach; ?> </table>update
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\captcha\Captcha; $form = ActiveForm::begin([ 'action' => 'index.php?r=zou2/updates', 'method' => 'post', ]) ?> <table align="center"> <tr> <td><a href="index.php?r=zou2/index">添加页面</a></td> <td><a href="index.php?r=zou2/lists">显示页面</a></td> </tr> <tr><td colspan="2"><?= $form->field($model, 'id')->hiddenInput(['value'=>$res['id']]) ?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'name')->textInput(['style'=>'width:250px','value'=>$res['name']]) ?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'pwd')->passwordInput(['style'=>'width:250px','value'=>$res['pwd']])?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'sex')->radioList(array(1=>'男',0=>'女'))?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'tel')->textInput(['style'=>'width:250px','value'=>$res['tel']])?></td></tr> <tr><td colspan="2"><?= $form->field($model, 'email')->textInput(['style'=>'width:250px','value'=>$res['email']] )?></td></tr> <tr><td colspan="2"><? echo $form->field($model, 'content')->textarea(['rows'=>4,'value'=>$res['content']]) ?></td></tr> <tr><td colspan="2"> <div class="form-group"> <div class="col-lg-offset-1 col-lg-11"> <?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?> </div> </div> </td> </tr> </table> <?php ActiveForm::end() ?>