当我尝试使用yii2框架上传多个图像,但在尝试保存到mysql时出现未定义的偏移量1错误 /*数据库异常–yii\db\Exception 未定义的偏移量:1 无法准备SQL:INSERT到'article'('atitle','alink','atext','aimage')值(:qp0,:qp1,:qp2,:qp3) ↵ 原因:yii\base\ErrorException 未定义的偏移量:1 在C:\wamp\www\yii\u yii\advanced\vendor\yiisoft\yii2\db\Command.php的第282行*/ $\u GET=[ 'r'=>'文章/创建', ]; $\邮政编码=[ “_csrf-backend'=>”UU9QSjdIRTE3DAApbxA/cjgMJjB.HnxrYQgmFQcZMUk1Fz0BdS42dg==”, “文章”=>[ “atitle”=>“tetst”, “alink”=>“tststs”, “atext'=>”

当我尝试使用yii2框架上传多个图像,但在尝试保存到mysql时出现未定义的偏移量1错误 /*数据库异常–yii\db\Exception 未定义的偏移量:1 无法准备SQL:INSERT到'article'('atitle','alink','atext','aimage')值(:qp0,:qp1,:qp2,:qp3) ↵ 原因:yii\base\ErrorException 未定义的偏移量:1 在C:\wamp\www\yii\u yii\advanced\vendor\yiisoft\yii2\db\Command.php的第282行*/ $\u GET=[ 'r'=>'文章/创建', ]; $\邮政编码=[ “_csrf-backend'=>”UU9QSjdIRTE3DAApbxA/cjgMJjB.HnxrYQgmFQcZMUk1Fz0BdS42dg==”, “文章”=>[ “atitle”=>“tetst”, “alink”=>“tststs”, “atext'=>”,mysql,yii2,yii2-advanced-app,Mysql,Yii2,Yii2 Advanced App,stststs, “目标”=>[ '', ], ], ]; $\u文件=[ “文章”=>[ “名称”=>[ “目标”=>[ ‘15826673_1248634195213700_156939437665228244_n.jpg’, “15826788_1436518559693668_578728510186279918_n.jpg”, ], ], “类型”=>[ “目标”=>[ “图像/jpeg”, “图像/jpeg”, ], ], “tmp_名称”=>[ “目标”=>[ 'C:\\wamp\

stststs

, “目标”=>[ '', ], ], ]; $\u文件=[ “文章”=>[ “名称”=>[ “目标”=>[ ‘15826673_1248634195213700_156939437665228244_n.jpg’, “15826788_1436518559693668_578728510186279918_n.jpg”, ], ], “类型”=>[ “目标”=>[ “图像/jpeg”, “图像/jpeg”, ], ], “tmp_名称”=>[ “目标”=>[ 'C:\\wamp\\tmp\\phpC5D0.tmp', 'C:\\wamp\\tmp\\phpC5E1.tmp', ], ], “错误”=>[ “目标”=>[ 0, 0, ], ], “大小”=>[ “目标”=>[ 41133, 143244, ], ], ], ]; $\u COOKIE=[ “高级后端”=>“hcfmm57n53gf926s0h3t3t3sh4”, “——”csrf-backend“=>”1ea9882e6d5d791210cbe0dec331cd5055604c7b0458fad44d0d603122e277c8a:2:{i:0;s:13:“——”csrf-backend;i:1;s:32:“FCPCXXZICVZIV9Z0GV_0QtxdXmKBfsG”, “_identity-backend”=>“f1c5c5cf00766c4f15699a32b4471626683a88a6c0fbaf695436b9b7e0c33093a:2:{i:0;s:17:_identity-backend;i:1;s:46:[3,“ceML8v46RVuqBLcbs61JEyT3zLcdI_ux”,2592000];”, ]; //以下是我的看法 //这是我的控制器

/*控制器*/

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model backend\models\Article */

$this->title = $model->aid;
$this->params['breadcrumbs'][] = ['label' => 'Articles', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="article-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->aid], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->aid], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'aid',
            'atitle',
            'alink',
            'atext',
            'aimage',  [

               'attribute' =>'aimage',
               'value'=>Yii::getAlias('@articlesImgUrl').'/'.$model->aimage, 
               'format'=>['image',['width'=>'100', 'height'=>'100']]


            ]
        ],

    ]) ?>

</div>

你能发布你的代码吗?显示更多代码。。显示如何获取文件数组并尝试存储它们。在控制器中显示视图代码和操作代码。这是我的视图“aimage”字段,在模型规则中应为“file”类型,但您将其定义为字符串。[['aimage'],'file'],请尝试此解决方案
<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model backend\models\Article */

$this->title = $model->aid;
$this->params['breadcrumbs'][] = ['label' => 'Articles', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="article-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->aid], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->aid], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'aid',
            'atitle',
            'alink',
            'atext',
            'aimage',  [

               'attribute' =>'aimage',
               'value'=>Yii::getAlias('@articlesImgUrl').'/'.$model->aimage, 
               'format'=>['image',['width'=>'100', 'height'=>'100']]


            ]
        ],

    ]) ?>

</div>
<?php

namespace backend\controllers;

use Yii;
use backend\models\Article;
use backend\ArticleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * ArticleController implements the CRUD actions for Article model.
 */
class ArticleController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /** 
     * Lists all Article models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ArticleSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Article model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Article model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {

        $model = new Article();

        if ($model->load(Yii::$app->request->post())) {

           $model->save(false);
           $aid = $model->aid;
           //echo $mid; 
           $model->aimage = UploadedFile::getInstances($model,'aimage');        


           foreach ($model->aimage as $imager) {
                    $image1 = new Article();
                     $aid1 = $image1->aid;
                    $imgName='article_' . $aid1 . '.' . $imager->getExtension();

                    if(!$image1->save()){
                        // handle the errors of model.
                    var_dump($image1->getErrors());
                    }
                    if ($image1->save()) {

                        $imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
                    }
                    else{}
                }


           //echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
          // $imgName='article_' . $aid . '.' . $imager->getExtension();
           //$imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
           //$model->aimage=$imgName;
           //$model->save();



            return $this->redirect(['view', 'id' => $model->aid]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Article model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {


            $model->save(false);
           $aid = $model->aid;
           //echo $mid; 
           $imager = UploadedFile::getInstance($model,'aimage');        
           //echo $imgName='article_' . $mid . '.' . $imager->getExtension(); exit;
           $imgName='article_' . $aid . '.' . $imager->getExtension();
           $imager->saveAs(Yii::getAlias('@articlesImgPath').'/'.$imgName);
           $model->aimage=$imgName;
           $model->save();


            return $this->redirect(['view', 'id' => $model->aid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Article model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Article model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Article the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Article::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "article".
 *
 * @property integer $aid
 * @property string $atitle
 * @property string $alink
 * @property string $atext
 * @property string $aimage
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['atitle', 'alink', 'atext', 'aimage'], 'required'],
            [['atitle'], 'string', 'max' => 50],
            [['alink', 'atext', 'aimage'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'aid' => 'Aid',
            'atitle' => 'Atitle',
            'alink' => 'Alink',
            'atext' => 'Atext',
            'aimage' => 'Aimage',
            [
            'atribute'=>'aimage',
            'value'=>Yii::getAlias('@articlesImgPath')          
            ]
        ];
    }
}