Bank registration via Ajax

Asked

Viewed 128 times

0

Good afternoon.

I’m a beginner in the world of programming, and I’m developing a web app and I’m having problems to register sales in the database. The system must insert new inputs to register more than 1 product per sale. When I press the sign-up button, it presents the following error:

Notice: Undefined variable: connect in C: xampp htdocs narguile Components cad_venda.php on line 13

Warning: mysqli_real_escape_string() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs narguile Components cad_venda.php on line 13

Notice: Undefined variable: connect in C: xampp htdocs narguile Components cad_venda.php on line 14

Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in C: xampp htdocs narguile Components cad_venda.php on line 14

Data Inserted

Just follow my form:

<div id="modal8" class="modal">

<div class="modal-content" id="design_modal">

    <div id="font_titulo_modal"> Cadastrar Venda </div>

    <div id="font_modal">

        <form name="venda" id="venda" action="components/cad_venda.php" method="POST">

            <div class="row" id="inputs_cad_venda">

                <div class="input-field col s8">

                    <input placeholder="Digite o produto" name="produto_venda" type="text" class="validate">
                    <label for="produto_venda">Produto</label>
                </div>

                <div class="input-field col s2">

                    <input placeholder="Digite a quantidade" name="quantidade_produto_venda" type="text" class="validate">
                    <label for="quantidade_produto_venda">Quantidade</label>
                </div>
            </div>

            <div id="inputs_adicionais"></div>

            <div class="row">

                <div class="col s4 center">

                    <button class="waves-effect waves-light grey darken-3 btn fonte_button2 modal-trigger" type="submit" name="cadastra_venda"><i class="material-icons left">add_circle</i><div class="espaco2">Cadastrar Venda</div></button>
                </div>
            </div>
        </form>

        <div class="col s2 center">

            <button class="btn-floating btn-large waves-light grey darken-3" id="add_produto_venda"><i class="material-icons">add</i></button>
        </div>
    </div>
</div>

Javascript in the header:

<script type="text/javascript">

$(document).ready(function(){  
var i=1;

$('#add_produto_venda').click(function(){

  i++;  
  $('#inputs_adicionais').append('<div class="row" id="inputs_cad_venda'+i+'"> <div class="input-field col s8"> <input placeholder="Digite o produto" name="produto_venda" type="text" class="validate"> <label for="produto_venda">Produto</label> </div> <div class="input-field col s2"> <input placeholder="Digite a quantidade" name="quantidade_produto_venda" type="text" class="validate"> <label for="quantidade_produto_venda">Quantidade</label> </div> </div>' ); 
}); 

$('#cadastra_venda').click(function(){

  $.ajax({

    url:"cad_venda.php",  
    method:"POST",  
    data:$('#venda').serialize(),

    success:function(data){

      alert(data);  
      $('#venda')[0].reset();  
    }  
  });  
});  
});   
</script>

And php:

<?php

$conecta = mysqli_connect('localhost', 'root', '');
mysqli_select_db($conecta, 'narguile');

$produto_venda = count($_POST['produto_venda']);

if($produto_venda > 0){

    for($i=0; $i<$produto_venda; $i++){

        if(trim($_POST["produto_venda"][$i] != '')){

            $sql = "INSERT INTO `vendas`(`desconto`) VALUES('".mysqli_real_escape_string($connect, $_POST["produto_venda"][$i])."')";  
            mysqli_query($connect, $sql);
        }
    }

    echo "Data Inserted";  
} else {

    echo "Please Enter Name";  
}

?>
  • Young man, the errors mentioned are in PHP. You put in the question HTML and Javascript. You have to solve the errors there in PHP.

  • Select line 13 of your cad_venda.php

  • Guys, sorry, I forgot to put the cad_venda.php... malz...

2 answers

2


You stated $conecta and then uses the $connect. Correct this part that will initially work.

Beginning of the code

$conecta = mysqli_connect('localhost', 'root', '');

Middle of code (With problem)

$sql = "INSERT INTO `vendas`(`desconto`) VALUES('".mysqli_real_escape_string($connect, $_POST["produto_venda"][$i])."')";  
mysqli_query($connect, $sql);

Correction

$sql = "INSERT INTO `vendas`(`desconto`) VALUES('".mysqli_real_escape_string($conecta, $_POST["produto_venda"][$i])."')";  
mysqli_query($conecta, $sql);
  • po, it worked, man, valeuzão....

1

The first "mistake" is not exactly a mistake, it is a news, a kind of warning. You’re just saying that a variable is undefined, that is, you don’t assign any value to it. To get rid of this "error" just put the error suppressor "@" before the variable, ex "@$var".

The second error, refers to the function mysqli_real_escape_string(), apparently you are passing parameters incorrectly, check this link how to make use of this function: http://php.net/manual/en/mysqli.real-escape-string.php

The third "error" is also news, just use the error suppressor.

The fourth and last error is also a parameter pass problem, check here how to use mysql_query function: http://php.net/manual/en/function.mysql-query.php

Contrary to what you are thinking, the error is not in JS, but in PHP.

  • Obg, sorry, I forgot to put php, but I just put

Browser other questions tagged

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