Skip to content
Advertisement

SQLSTATE[22007]: Invalid datetime forma

I try to save some data that it brings me from my view, which is a table, but I don’t know why it throws me that error with the insert. result of insert

this is my view: table of view

this is my controller:

    $checked_array = $_POST['id_version'];


    foreach ($request['id_version'] as $key => $value) {
        if (in_array($request['id_version'][$key], $checked_array)) 
        {

            $soft_instal = new Software_instalacion;
            $soft_instal->id_instalacion             = $instalaciones->id;
            $soft_instal->id_historial               = $historial->id;
            $soft_instal->id_usuario                 = $request->id_usuario;
            $soft_instal->id_version                 = $_POST['id_version'][$key];
            $soft_instal->obs_software               = $_POST['obs_software'][$key];
            $soft_instal->id_tipo_venta              = $_POST['id_tipo_venta'][$key];
            $soft_instal->save();
        }
    }

Advertisement

Answer

id_tipo_venta seems to be an empty string which is apparently not valid.

You can try debugging what you get in :

var_dump($_POST['id_tipo_venta'][$key]);
die;

Your database field expects to receive an integer. Therefore, using the intval() function can solve your problem.

Indeed, I think your code returns an alphanumeric string.

Therefore, the code below will return 0 in all cases if no version is returned (not set, string or simply null):

$soft_instal->id_tipo_venta = intval($_POST['id_tipo_venta'][$key]);

On the other hand, intval() will always convert to int, so a decimal will be converted, example :

intval("1.1") // returns 1
intval("v1.1") // returns 0

If this is not the desired behavior, maybe you should think about changing your database type.

EDIT : Of course, you can also set the value as null if you prefer to 0. You must allow nullable values in your database.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement