How to resolve message Illegal offset type

1

I'm getting this error message:

  

PHP Warning: Illegal offset type in E: \ home \ topdeia \ Web \ n-chipi \ update-data.php on line 9

The code I have is this:

session_start();

$acao = $_POST['acao'];

if ( isset($acao) && $acao == 'atualizar-quantidade' ){
    $id = isset($_POST["id_qtd"]) ? $_POST["id_qtd"] : null;
    $_SESSION["quantidade"][$id] = $_POST['quantidade'];        
}

Following a tip, I did this, I added (string)

session_start();

$acao = $_POST['acao'];

if ( isset($acao) && $acao == 'atualizar-quantidade' ){
    $id = isset($_POST["id_qtd"]) ? $_POST["id_qtd"] : null;
    **$_SESSION["quantidade"][$id] = (string)$_POST['quantidade'];**        
}

The message persists.

    
asked by anonymous 18.04.2017 / 19:15

1 answer

1

Everything indicates that the $id variable is not a valid index for the $_SESSION["quantidade"] array.

Do this to only run this line if it is a valid index.

session_start();

$acao = $_POST['acao'];

if ( isset($acao) && $acao == 'atualizar-quantidade' ){
    $id = isset($_POST["id_qtd"]) ? $_POST["id_qtd"] : null;
    if (isset($_SESSION["quantidade"][$id])) {
        $_SESSION["quantidade"][$id] = (string)$_POST['quantidade'];        
    }
}
    
18.04.2017 / 20:36