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',
];
}
}