duanguan1573 2017-02-17 00:08
浏览 38
已采纳

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

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)

I have that error when trying to put data from form to my yii2 database. I created model for that purpose, created action in my controller and view with ActiveForm to send data. Now I just need to get data from that form and put that into my db. Error occur in this line:

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

it's one line for my controller action:

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,
    ));
}

my requests data:

$_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,
            ],
        ],
    ],
];

and I have model generated through 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条回答 默认 最新

  • dongxiusuo9881 2017-02-19 02:21
    关注

    The problem is that you send the images value in the post is an array. You have to json encode the value before you save it to the database.

    In your Form model override the load function:

    public function load($data, $formName = null)
        {
            $scope = $formName === null ? $this->formName() : $formName;
            if($scope) {
                $data[$scope]['images'] = \yii\helpers\Json::encode($data[$scope]['images']);
            }
            else {
                $data['images'] = \yii\helpers\Json::encode($data['images']);
            }
            return parent::load($data, $formName); // TODO: Change the autogenerated stub
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题