How to store a null array?

0

I have an array that is generated when choosing items from a multiple select. In the process of recording data I do this:

if (isset($_POST['temas_event'])) {
   $temas_event = json_encode($_POST['temas_event']);
} else {
   $temas_event = ['0'];
}

What I have noticed is that although in the base it is defined as the default value "NULL" there are times that if I do not select anything in the multiple select, in the database it records the word "Array" with the consequent ones problems that I generate when reading the records ... that is to say that my definition of the registry ignores it or is ill-defined. What do you suggest I can do?

The recording process is done as:

$conexion = new Conexion();
        $stmt = $conexion -> prepare("UPDATE usuarios_full SET nombre_event = :nombre_event, dom_event = :dom_event, lat_event = :lat_event, lng_event = :lng_event, web_event = :web_event, tel_event = :tel_event, alcance_event = :alcance_event, temas_event = :temas_event, cert_event = :cert_event, event_pagos = :event_pagos WHERE idusuario = :valor");

        $stmt->bindValue(":valor", $idusuario);

        $stmt->bindValue(":nombre_event", $nombre_event);
        $stmt->bindValue(":dom_event", $dom_event);
        $stmt->bindValue(":lat_event", $lat_event);
        $stmt->bindValue(":lng_event", $lng_event);
        $stmt->bindValue(":web_event", $web_event);
        $stmt->bindValue(":tel_event", $tel_event);
        $stmt->bindValue(":alcance_event", $alcance_event); (*******)
        $stmt->bindValue(":temas_event", $temas_event);   (*******)
        $stmt->bindValue(":cert_event", $cert_event);
        $stmt->bindValue(":event_pagos", $event_pagos);
        $stmt->execute();

        if ($stmt->rowCount() > 0) {
            $resultado = 1;
        } else {
            $resultado = NULL;
        }

where (*******) are arrays

    
asked by MNibor 01.08.2017 в 20:18
source

1 answer

1

In the process of recording , if the variable is not set, you could initialize it as array() .

So for example:

$valor = isset($_POST['temas_event'])? $_POST['temas_event'] : array();
$temas_event = json_encode($valor);

On the other hand, the word Array , appears because in the else of your logic you are creating an array and you are not encoding it ( eg: You are not doing it json_encode )

UPDATE

Since you want to save null or% string , then you should do it like this:

$valor = isset($_POST['temas_event'])? json_encode($_POST['temas_event']) : null;
$temas_event = $valor;

Because you are using PDO and you need to save NULL or% string , it is necessary that at the time of "bindear" the value of the variable $temas_event , you indicate different types of data according to value.

So for example:

// ....
$type = $temas_event === null ? PDO::PARAM_INT : PDO::PARAM_STR;
$stmt->bindValue(":temas_event", $temas_event, $type);
// ....

PD : You could use PDO::PARAM_NULL for the case where $temas_event === null , but comment that in some environments PDO::PARAM_NULL , it does not work correctly

    
answered by 01.08.2017 / 20:33
source