Naj's avatar
Level 1

General error: 1366 Incorrect integer value

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 ?

0 likes
4 replies
rickbolton's avatar
Level 8

It's because you are trying to save a string into an integer field.

In your migration change importance to a string like

 $table->string('importance'); 

Or you will need to change the value of your radio button to an integer

1 like
otepas's avatar

Hi @Naj, the value you are putting in the radio buttons is a string "important" or "tres_important", but the DB field is an integer. That is why it is failing, when Laravel tries to store a string in an integer field.

You have to either use a numeric value in the radio buttons or change the DB field to be a string.

1 like
Naj's avatar
Level 1

yes, that's what I did, I've changed the value of radio button into integer :

<input type="radio" id="tres_important" name="importance" value="0t">Très important</label>

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

What if, I had 3 choices in radio button, can the values be 0,1,2, or it is only binary

Please or to participate in this conversation.