How to resolve Illegal offset type message

Asked

Viewed 3,960 times

1

I’m getting this error message:

PHP Warning: Illegal offset type in E: home topdeia Web n-chipi atualizar-dados.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.

  • What is the line 9 in that stretch ? Look who you assign conditionally null to the variable $id and then use it as an index.

  • Hello @Edilson, line 9 is marked with asterisks in the second code snippet.

  • 2

    The logic would not be, if there is no id_qntd not proceed with the change ?

1 answer

1


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

Do so only to 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'];        
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.