How to verify variable has values?

Asked

Viewed 78 times

0

I have a function that performs a select on the bank:

function selectIdProdutos($id){
    $banco = abrirBanco();
    $sql = " SELECT * FROM produtos WHERE id = ".$id;
    $resultado = $banco->query($sql);
    $banco->close();
    $produto = mysqli_fetch_assoc($resultado);
    return $produto;
}

However, when there are no records in the database it returns:

Notice: Undefined variable: produtos in `C:\xampp\htdocs\despesas\despesas\registra_produto.php on line 63`

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\despesas\despesas\consulta_produtos.php on line 86

To show the products I use:

<tbody>
                <?php

                foreach($grupo as $produtos){ ?>

                    <tr>
                        <td><?=$produtos["descricao"]?> </td>
                        <td>R$<?=$produtos["custo"]?></td>
                        <td>R$<?=$produtos["preco_venda"]?></td>
                        <td> <?php
                            if($produtos["fg_ativo"] == '1'){
                                $produtos["fg_ativo"] = "Ativo";
                            }
                            else{
                                $produtos["fg_ativo"] = "Inativo";
                            }
                        ?>
                        <?=$produtos["fg_ativo"]?> </td>
                        <td>

                            <form name="alterar" action="alterar_produtos.php" method="POST">
                                <input type="hidden" name="id" value= <?=$produtos["id"]?> />
                                <input type="submit" value="Editar" name="editar" class="btn btn-default">
                            </form>

                        </td>
                        <td>

                            <form name="excluir" action="registra_produto.php" method="POST">

                                <input type="hidden" name="id" value="<?=$produtos["id"]?> " />
                                <input type="hidden" name="acao" value="excluir" />
                                <input type="submit" value="Excluir" name="excluir" class="btn btn-default" />

                            </form>

                        </td>
                    </tr>

                    <script>
                        function msgSucesso(){
                            alert('Produto excluido com sucesso');
                        }
                    </script>


                <?php

                }

                ?>

How can I treat it?

  • 1

    You can test the variable using the ! Empty - Empty Function condition. A variable is considered empty if it does not exist or its value is FALSE. The function Empty() does not generate a warning if the variable does not exist.

  • 1

    Put the part of the code where you arrow the value of the products variable the errors that this giving is referring to variable not this defined and not referring to its values

  • My answer was helpful in your question?

1 answer

0

There are several ways to do this:

Has a php function called isset() it helps us to check whether the variavel for parametro is setada, or contain some value. By Ex:

if(isset($_POST['nome'])){
   $nome = $_POST['nome'];
}else{
   $nome = '';
}

or if it has value in name, it assigns the variable name, if not put it as empty.

another way is by using the Function Empty (click to see documentation).

Determines whether a variable is considered empty. A variable is considered empty if it does not exist or its value is equal to FALSE. Function Empty() does not generate a warning if the variable does not exist.

for example:

if(empty($_POST['nome'])){
    $nome = 'vazio';
}else{
    $nome = $_POST['nome'];
}

or if the name is empty, empty assigns the variable name, if not put it the name value.

Remembering that all these functions you can assign the ternary operator ! to do otherwise inside an if. for example:

  if(!isset()) // se nao tiver setado

  if(!empty()) // se nao tiver vazio

Browser other questions tagged

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