filter_input GET

0

I need to retrieve the value of GET to perform an insertion in the database through the post method, however when performing call of the function through the submit, it presents next critical "Call Stack".

✓ Code

<?php
    session_start();
    if(empty($_SESSION['id'])){
        $_SESSION['msg'] = "Área restrita. Faça login" ;
        header("Location: index.php");
    }
    include_once("include/config.php");

    $btnOrca = filter_input(INPUT_GET, 'btnOrca', FILTER_SANITIZE_STRING);

    if($btnOrca){
        echo "Entrou na função";

            //Query Insert
            $query = "INSERT INTO orcamento (num_orcamento, produto, qtde,valor_unitario,valor_total) VALUES ('".$_GET['num_orc']."', '".$_GET['produto']."', '".$_GET['qtde']."', '".$_GET['valor_unit']."','".$_GET['total']."')";

            //Connection BD
            $result = mysqli_query($conn, $query);

            if($result){
                $_SESSION['msg'] = "Orçamento Gravado com Sucesso.";
            }else{
                $_SESSION['msg'] = "Problema ao Gera Orçamento";
            }
        }
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <title>Compra</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/stylo.css">
</head>
<body>
    <script src="js/menu.js"></script>
    <br><br><br>
    <?php
        if(isset($_SESSION['msg'])){
            echo "<p style='color: red;'>".$_SESSION['msg']."</p>";
            unset($_SESSION['msg']);
        }
    ?>
    <p class="title">Orçamentos</p>
    <form method="get" action="" class="container" style="width: 400px;">
    <label for="orcamento">Numero do Orçamento:</label>
        <input type="number" id="num_orc" name="num_orc" min="1" value="1" class="form-control"/>   
        <br>    
        <label for="produto">Produto</label>
        <select id="produto" name="produto" class="form-control">
            <?php
                $query = "SELECT id,descricao FROM produto";
                $resultado = mysqli_query($conn,$query);
                if($resultado){
                    while($linha = mysqli_fetch_assoc($resultado)){
                        echo "<option value='".$linha['id']."'>".$linha['descricao']."</option>";
                    }
                }
            ?>
        </select>
        <br>
        <label for="qtde">Quantidade: </label>
        <input type="number" id="qtde" name="qtde" min="1" value="1" class="form-control"/>
        <br>
        <label for="valor_unit">Valor unitário: </label>
        <input type="number" id="valor_unit" name="valor_unit" step="0.01" value="0.01" class="form-control" onblur="calcular()" />
        <br>
        <label for="total">Valor Total: </label>
        <input type="number" id="total" name="total" class="form-control" disabled/>
        <br>
        <input type="submit" class="btn btn-primary" name="btnOrca" value="Gravar" />
    </form>
<!-- Javascript calcuclar Campo -->
<script>
function calcular() {
  var n1 = parseInt(document.getElementById('valor_unit').value, 10);
  var n2 = parseInt(document.getElementById('qtde').value, 10);
  var mone = (n1 * n2);
  document.getElementById('total').value = mone;
}
</script>    
</body>
</html>

Error

    
asked by anonymous 18.11.2017 / 20:59

2 answers

0

Change here:

 $btnOrca = filter_input(INPUT_GET, 'btnOrca', FILTER_SANITIZE_STRING);

if($btnOrca){
    echo "Entrou na função";

To:

i$btnOrca = filter_input_array(INPUT_GET, FILTER_DEFAULT);

if($btnOrca['btnOrca']){
// aqui vai o contudo do if.
}

Your query will look like this:

    $query = "INSERT INTO orcamento (num_orcamento, produto, qtde,valor_unitario,valor_total) VALUES ('".$btnOrca['num_orc']."', '".$btnOrca['produto']."', '".$btnOrca['qtde']."', '".$btnOrca['valor_unit']."','".$btnOrca['total']."')";

The GET vows can be picked up like this: $ btnOrca ['field name'], so you can use them as you like.

I hope I have helped.

    
18.11.2017 / 22:27
0

It seems like it's giving the MySQL insert problem, are all the fields all of the same string? If they are trying to give a filter for each GET, maybe right. Now if any field is integer, you need to pass the data as number and not as text.

In MySQl when you put the values in quotation marks you enter as text. Example '1' is inserted as text (string) and 1 without quotation marks is entered as integer.

Maybe the filters help:

$num_orc = trim(filter_var($_GET['num_orc'], FILTER_SANITIZE_STRING));
$produto = trim(filter_var($_GET['produto'], FILTER_SANITIZE_STRING));
$qtde = trim(filter_var($_GET['qtde'], FILTER_SANITIZE_STRING));
$valor_unit = trim(filter_var($_GET['valor_unit'], FILTER_SANITIZE_STRING));
$total = trim(filter_var($_GET['total'], FILTER_SANITIZE_STRING));

$query = "INSERT INTO orcamento (num_orcamento, produto, qtde, valor_unitario, valor_total) VALUES ('{$num_orc}', '{$produto}', '{$qtde}', '{$valor_unit}','{$total}')";

If they are integer types, try changing FILTER_SANITIZE_STRING to FILTER_SANITIZE_NUMBER_INT

example:

$num_orc = filter_var($_GET['num_orc'], FILTER_SANITIZE_NUMBER_INT);

And in the mysql insert, remove the quotation marks as follows:

... valor_total) VALUES ({$num_orc}, ...

Hope this helps you solve your problem. If you continue to give an error, please update the question with a new print of the errors that appear.

    
19.11.2017 / 08:05