我的yii2网站中未定义的偏移1

时间:2017-02-17 00:08:25

标签: php yii yii2

Failed to prepare SQL: INSERT INTO `form` (`name`, `email`, `age`, `height`, `weight`, `city`, `techies`, `english_level`, `images`) VALUES (:qp0, :qp1, :qp2, :qp3, :qp4, :qp5, :qp6, :qp7, :qp8)

我在尝试将数据从表单放到我的yii2数据库时遇到了这个错误。我为此创建了模型,在我的控制器中创建了动作,并使用ActiveForm查看以发送数据。现在我只需要从该表单中获取数据并将其放入我的数据库中。 此行发生错误:

if (($loaded = $Form->load(Yii::$app->request->post())) && $Form->save())

它是我控制器操作的一行:

public function actionForm()
{
    $Form = new Form();
    if(!$Form->validate()){
        var_dump("fuck");
    }
    if (($loaded = $Form->load(Yii::$app->request->post())) && $Form->save()) {
                return 'ok';
    }elseif($loaded){
 var_dump($Form->getErrors());
 }

    return $this->render('form',array(
       'Form' => $Form,
    ));
}

我的请求数据:

$_GET = [
    'r' => 'site/form',
];

$_POST = [
    '_csrf' => 'MGhXNV82cTdkBTFPC3k4VAVRZUEoBDxZUi4cRSpnAUZaMTxxbwZEBQ==',
    'Form' => [
        'name' => 'Rost',
        'email' => 'prozrostl@gmail.com',
        'age' => '22',
        'height' => '12',
        'weight' => '1212',
        'city' => '1212',
        'techies' => 'yes_camera',
        'english_level' => 'starter',
        'images' => [
            '',
        ],
    ],
];

$_FILES = [
    'Form' => [
        'name' => [
            'images' => [
                'user.png',
            ],
        ],
        'type' => [
            'images' => [
                'image/png',
            ],
        ],
        'tmp_name' => [
            'images' => [
                'W:\\XAMPP\\tmp\\php649A.tmp',
            ],
        ],
        'error' => [
            'images' => [
                0,
            ],
        ],
        'size' => [
            'images' => [
                445,
            ],
        ],
    ],
];

我通过gii生成模型。

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "form".
 *
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property integer $age
 * @property integer $height
 * @property integer $weight
 * @property string $city
 * @property string $techies
 * @property string $english_level
 * @property string $images
 * @property resource $photo_1
 * @property resource $photo_2
 * @property resource $photo_3
 * @property resource $photo_4
 * @property resource $photo_5
 */
class Form extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'form';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'email', 'age', 'height', 'weight', 'city', 'techies', 'english_level'], 'required'],
            [['name', 'email', 'city', 'techies', 'english_level'], 'string'],
            [['images', 'photo_1', 'photo_2', 'photo_3', 'photo_4', 'photo_5'], 'file'],
            [['age', 'height', 'weight'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'email' => 'Email',
            'age' => 'Age',
            'height' => 'Height',
            'weight' => 'Weight',
            'city' => 'City',
            'techies' => 'Techies',
            'english_level' => 'English Level',
            'photo_1' => 'Photo 1',
            'photo_2' => 'Photo 2',
            'photo_3' => 'Photo 3',
            'photo_4' => 'Photo 4',
            'photo_5' => 'Photo 5',
        ];
    }
}

1 个答案:

答案 0 :(得分:0)

问题是你在帖子中发送的图像值是一个数组。在将值保存到数据库之前,必须对值进行json编码。

在表单模型中覆盖加载函数:

Cougar