Error doing Insert in PHP database

Asked

Viewed 1,080 times

1

I am trying to do the data Insert in Mysql, no error appears but does not insert the values in the database.

    <?php

        INCLUDE "conexao.php";


        if (isset($_POST["submit"])) {

        $cod_produto = $_POST['cod_produto'];
        $dsc_produto = $_POST['dsc_produto'];
        $preco_produto = $_POST['preco_produto'];
        $qtd_estoque = $_POST['qtd_estoque'];



        $sql = "INSERT INTO estoque (cod_produto, dsc_produto, preco_produto, qtd_estoque) VALUES ('".$cod_produto."', '".$dsc_produto."', '".$preco_produto."', '".$qtd_estoque."')";
            if (!mysqli_query($sql)) {
            die('Error: ' . mysqli_error()); 
        }

        else
            echo" error";
        }

        ?>

I already tested the connection and it’s ok.


With the help of the staff I was able to find the mistakes.

  1. Submit was not working, correct code:

    <button id="btn_aceitar" name="submit" value="submit" class="btn btn-success" type="Submit">Adicionar produto</button>
    
  2. was forgetting to call the query itself, correct code:

        <?php
    
        INCLUDE "conexao.php";
    
    
        if (isset($_POST["submit"])) {
    
        $cod_produto = $_POST['cod_produto'];
        $dsc_produto = $_POST['dsc_produto'];
        $preco_produto = $_POST['preco_produto'];
        $qtd_estoque = $_POST['qtd_estoque'];
    
    
    
        $sql = "INSERT INTO estoque (cod_produto, dsc_produto, preco_produto, qtd_estoque) VALUES ('".$cod_produto."', '".$dsc_produto."', '".$preco_produto."', '".$qtd_estoque."')";
        $mysql = mysqli_query($conexao,$sql);
            if (!mysql) {
            die('Error: ' . mysqli_error()); 
        }
    
        else {
            echo "Completo";
        }
        }
        else {
            echo "submit error";
        }
    
        ?>
    

Thanks a lot.

  • In your code, if it is true it presents error and if it is false too. The data is entered?

  • If you solved the problem, you can create an answer explaining the details.

2 answers

2

  • Good! Actually I forgot to call the function itself, I edited my question with the corrected code.

0


Your code checks if $_POST["Submit"] is true.

It is the only part of your code that, if false, shows no error. Make sure some value is being passed to $_POST['Ubmit'].

  • Good! I think I have some error in Submit. To avoid Injection, I should use an Insert filter instead?

  • To check for a post type request, you can simply use an if($_POST). To avoid injections you can use this function (https://github.com/jrossetto/php/blob/master/functions/antiInjection.php) applied by an array_map in the $_POST array.

  • I’ll do some research, thank you.

Browser other questions tagged

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