douquan9826 2017-01-18 16:09
浏览 92
已采纳

常规错误:1366不正确的整数值:laravel中的单选按钮

I am facing this error while trying to submit my form : General error: 1366 Incorrect integer value : 'important' for column 'importance' at row 1 (SQL: insert into projets (title, dateDebut, dateFin, cout, partenaires_financiers, importance, updated_at, created_at) values (MyTest, 1996/08/15, 1998/06/28, 15900, paldk ksjd, important, 2017-01-18 15:31:57, 2017-01-18 15:31:57))

Here's the code I am using :

submit.blade.php :

<label for="Import">Importance</label>
<div class="form-group"> 

 <label class="radio-inline">
 <input type="radio" id="tres_important" name="importance" value="tres_important">Très important</label>

<label class="radio-inline">
<input type="radio" id="important" name="importance" value="important">Important</label>
</div>

the migration "create_projects_table.php" :

class CreateProjetsTable extends Migration
{

public function up()
{
Schema::create('projets', function (Blueprint $table) {
    $table->increments('id',true);
    $table->string('title');
    $table->date('dateDebut');
    $table->date('dateFin');
    $table->float('cout');          
    $table->string('partenaires_financiers');
    $table->integer('importance'); 
    $table->timestamps();
 });
 }   

"route.php" :

Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/', function () {
$projets = \App\Projet::all();
return view('welcome', compact('projets'));
});

Route::get('/submit', function () {
   return view('submit');
});
Route::post('submit/projects', 'Project_Controller@store');

"Project_Controller.php" :

public function store(Request $request)
{

$validator = Validator::make($request->all(), [
    'title' => 'required|max:255',
    'dateDebut' => 'required|max:255',
    'dateFin' => 'required|max:255',
    'cout' => 'required|max:255',
    'importance' => 'required',
    //'etude' => 'required',
    'partenaires_financiers' => 'required',

]);

if ($validator->fails()) {
 return back()
    ->withInput()
    ->withErrors($validator);
}     

$projet = new Projet;

$projet->title = $request->title;
$projet->dateDebut = $request->dateDebut;
$projet->dateFin = $request->dateFin;
$projet->cout = $request->cout;
$projet->partenaires_financiers = $request->partenaires_financiers;
$projet->importance = $request->importance;

$projet->save();

return redirect('/submit');
}
}

I was using this code in my controller before adding the "store" function and it was working :

$create_projets = CreateProjets::find($id);
$data['importance'] = $create_projets->importance;
return view('submit', $data);

I know that the error is caused by the "radio buttons"

Does anyone have an idea how to fixe that please ?

  • 写回答

1条回答 默认 最新

  • doulongdan2264 2017-01-18 16:19
    关注

    in your create_projects_table you want to change

    $table->integer('importance'); to $table->string('importance');

    or if imporantance is binary value like 0/1

    $projet->importance = $request->importance === 'important' ? 1 : 0; Would assign importance to be 1 if the radio selected as important. (not sure if tres important is more or less important)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?