Error in Javascript

Asked

Viewed 130 times

1

Could someone tell me how to JOIN to fill the data automatically from the description field. Accuses this error Notice: Trying to get property of non-object in C:\xampp\htdocs\PhpProject1\function.php on line 9 {"codigo_produto":"","barcode":"","porcao":""} in the case of line 9 is this if($resultado->num_rows){

index php.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    </head>
    <body>
            <script type='text/javascript'>
            $(document).ready(function(){
                    $("input[name='descri']").blur(function(){
                            var $codigo_produto = $("input[name='codigo_produto']");
                            var $barcode = $("input[name='barcode']");
                            var $codigo_tipo = $("input[name='codigo_tipo']");
                            var $porcao = $("input[name='porcao']");

                            $.getJSON('function.php',{ 
                                    descricao: $( this ).val() 
                            },function( json ){
                                    $codigo_produto.val( json.codigo_produto );
                                    $barcode.val( json.barcode );
                                    $codigo_tipo.val ( json.val );
//                                    $porcao.val ( json.val );
                            });
                    });
            });
            </script>   
            <div class="col-lg-5"><!-- Inicio Input Cóodigo do Produto-->
                <label for="ex1">Descrição:</label>
                <input type="text" required class="form-control" maxlength="13" name="descri"><br>
            </div><!-- Fim Input Código do Produto -->
            <div class="col-lg-4"><!-- Inicio Input Cóodigo do Produto-->
                <label for="ex1">Código do Produto:</label>
                <input type="text" required class="form-control" maxlength="13" name="codigo_produto"><br>
            </div><!-- Fim Input Código do Produto -->

            <div class="col-lg-5"><!-- Inicio Input Código EAN / Barcode -->
                <label for="ex1">Código EAN:</label>
                <input type="text" required class="form-control" maxlength="13" name="barcode"><br>
            </div><!-- Fim Input Código EAN / Barcode -->          

            <div class="col-lg-6">
                <label for="ex1">Tipo:</label>
                <input type="text" class="form-control" name="codigo_tipo" value="">
            </div>

            <div class="col-lg-6">
                <label for="ex1">Porção:</label>
                <input type="text" class="form-control" maxlength="20"  name="porcao">
            </div>
    </body>

Function.php

   function retorna($descricao, $conn){

        $result = "SELECT a.descricao, a.codigo_produto, a.barcode, b.codigo_tipo, c.porcao FROM CADPRO A
            LEFT OUTER JOIN TIPOPROD B ON (A.CODIGO_PRODUTO = B.CODIGO_PRODUTO)
            LEFT OUTER JOIN INFOPROD C ON (B.CODIGO_PRODUTO = C.CODIGO_PRODUTO) WHERE descricao =";
        $resultado = mysqli_query($conn, $result);

        if(!is_null($resultado)){
            $row = mysqli_fetch_assoc($resultado);
                $valores['codigo_produto'] = $row['codigo_produto'];
                $valores['barcode'] = $row['barcode'];
                $codigo_produto = $row['codigo_produto'];                    
                $valores['codigo_tipo'] = $row['codigo_tipo'];
                $valores['porcao'] = $row['porcao'];
        } else {
                $valores['codigo_produto'] = '';
                $valores['barcode'] = '';
                $valors['tipo'] = '';
                $valores['porcao'] = '';             
        }

        return json_encode($valores);                
    }
    if(isset($_GET['descricao'])){
        echo retorna($_GET['descricao'], $conn);
    }
    ?>
  • Defiz the edition, otherwise the answer given would lose its meaning.

2 answers

0

Try to put it this way:

$resultado = mysqli_num_rows($resultado);

if(!is_null($resultado)){
     ...

Assuming you didn’t urge class mysqli. Take the example in this page, interpreting your code, apparently you’re doing it procedural.

This way I could not use any type of object.

  • I will show the code in Function.php as this now my error is -----><b>Warning</b>: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in <b>C: xampp htdocs Phpproject1 Function.php</b> on line <b>14</b><br /> {"product code":null,"Barcode":null,"type code":null,"nut":null}

  • The function mysqli_fetch_assoc returns an associative matrix that corresponds to the obtained line, or NULL if there are no more lines. In this case, since you are not using mysqli_num_rows, you will return NULL. (On your last update)

0


That makes no sense:

if(!is_null($resultado)){

The mysqli_query will never return NULL, it will always fall in the same if, even if it gives error, in case it returned false and how false is not null he enters the first if anyway, but the mysqli_fetch_assoc always expects an "object".

To avoid this is just always use the documentation to be sure how the function works and will see http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues.

See the documentation says it returns FALSE if it fails or a object mysqli_result if successful.

Change the code to:

if($resultado){
    $row = mysqli_fetch_assoc($resultado);
        $valores['codigo_produto'] = $row['codigo_produto'];
        $valores['barcode'] = $row['barcode'];
        $codigo_produto = $row['codigo_produto'];                    
        $valores['codigo_tipo'] = $row['codigo_tipo'];
        $valores['porcao'] = $row['porcao'];
} else {

Another suggestion is to create an error JSON, like this:

if($resultado){
    $row = mysqli_fetch_assoc($resultado);
        $valores['codigo_produto'] = $row['codigo_produto'];
        $valores['barcode'] = $row['barcode'];
        $codigo_produto = $row['codigo_produto'];                    
        $valores['codigo_tipo'] = $row['codigo_tipo'];
        $valores['porcao'] = $row['porcao'];
} else {
       return json_encode(array( 'error' => mysqli_error($conn) ));
}

Another important, you did not declare the variable $values, but declared the variable $codigo_produto = $row['codigo_produto'];, but you’re not using it, maybe the code should look like this:

function retorna($descricao, $conn){

    $result = "SELECT a.descricao, a.codigo_produto, a.barcode, b.codigo_tipo, c.porcao FROM CADPRO A
        LEFT OUTER JOIN TIPOPROD B ON (A.CODIGO_PRODUTO = B.CODIGO_PRODUTO)
        LEFT OUTER JOIN INFOPROD C ON (B.CODIGO_PRODUTO = C.CODIGO_PRODUTO) WHERE descricao =";
    $resultado = mysqli_query($conn, $result);

    // DECLARA A VARIAVEL
    $valores = array();

    if(!is_null($resultado)){
        $row = mysqli_fetch_assoc($resultado);
        $valores['codigo_produto'] = $row['codigo_produto'];
        $valores['barcode'] = $row['barcode'];
        $codigo_produto = $row['codigo_produto'];                    
        $valores['codigo_tipo'] = $row['codigo_tipo'];
        $valores['porcao'] = $row['porcao'];
    } else {
        return json_encode(array( 'error' => mysqli_error($conn) ));        
    }

    return json_encode($valores);                
}
  • In the case your code presents this error when debugging ---> <b>Notice</b>: Undefined variable: values in <b>C: xampp htdocs Phpproject1 Function.php</b> on line <b>22</b><br />

  • @Victort. this ae is another error, it is just you notice, you did not declare the variable $values within the scope of the function. And if you notice correctly you declared the variable $codigo_produto, but you’re not wearing it.

  • @Victort. I edited the answer, see the example code of the end you can understand.

  • so in this case, I find strange the fact that I test the same code without Join using it to bring data from a single table and it works, strange this, I will edit the question the file funcition.php to check, is identico

  • 1

    @Victort. one thing has nothing to do with the other, Join is in the mysql command, your errors were in PHP, see the edit of the answer ....... If you want to understand the basics of JOIN read this: https://answall.com/a/6448/3635

  • the problem is not the same JOIN, but very strange PHP does not recognize, since I play this JOIN in MYSQL and it works

  • 1

    @Victort. not that, PHP is PHP, mysql is mysql... they do not work parallel, the problem is how you are doing the JOIN that will generate a different output, as I sent you in the linkp to you understand, each Join has an effect, understanding the effect you already understand part of the result, if playing in a program like Heidsql or phpmyadmin will notice that it probably generated repeated lines, ie it is you who do not understand well the effect of JOIN and this making confusion, it is not the fault of PHP.....

  • .... You will probably want to add values or group something, and I have no way to answer you because I don’t know what your table looks like or the data it contains. @Victort.

  • i want from the description that the user type, as long as it tab the remaining data are filled automatically

  • @Victort. ae you’re already changing the direction of the question totally, I solved the problem here, right? The part that was the mistake Trying to get property of non-object. Now if you have another doubt even more complex so create a new question and explain in detail what happens.

  • not my problem continues, but I will mark your answer as the best yet by the effort, JUST READ THE QUESTION --->Could someone tell me how to do JOIN to fill the data automatically from the description field.

  • @Victort. continues the same error message Trying to get property of non-object or Undefined variable: valores?

  • the error is now ----><b>Warning</b>: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in <b>C: xampp htdocs Phpproject1 Function.php</b> on line <b>15</b><br /> {"product code":null,"Barcode":null,"codigo_type":null,"nut":null} BUT I EDITED MY FUNCTION AND THIS ONE IDENTICAL TO YOURS, IF I TAKE THE ! is_null ME returns ONLY [] on NETWORK

  • @Victort. are you sure you set the code like I told you? I think you made another mess.

  • edited the Function again

  • @Victort. So it’s without is_null, I’ve made it very clear in my answer, if you’ve added again it’s because you don’t understand what you’re doing, you’re doing random and meaningless things, simply. I have already said that it will never return null, so it makes no sense to use is_null. And if without is_null it is returning an empty array [] is because there is something wrong in your query, but as I already told you to this case you should open a new question, because this is another problem, as details of the table and data, because without it I have no way to help you without information of this type.

  • ok thanks, I will try to open another question, but I made some edits in the query and now it says that --> "Column 'Description' in Where clause is ambiguous"} it’s bad to be beginner rs

  • @Victort. this is a problem in your query itself, when you ask the question send me the link

  • @Victort. Before creating the question, try to change to this $result = "SELECT a.descricao, a.codigo_produto, a.barcode, b.codigo_tipo, c.porcao FROM CADPRO A&#xA; LEFT OUTER JOIN TIPOPROD B ON (A.CODIGO_PRODUTO = B.CODIGO_PRODUTO)&#xA; LEFT OUTER JOIN INFOPROD C ON (B.CODIGO_PRODUTO = C.CODIGO_PRODUTO) WHERE a.descricao =''"; and see if it works.

  • I did so with a.Description and the result when I inspect and in the Network tab is this =>{"product code":"MARMEL","Barcode":"3719826450","type code":"PEDACO","nut":"50 g"} $result = "SELECT a.descricao, a.codigo_produto, a.barcode, b.codigo_tipo, c.porcao FROM CADPRO A&#xA;LEFT OUTER JOIN TIPOPROD B ON (A.CODIGO_PRODUTO = B.CODIGO_PRODUTO)&#xA;LEFT OUTER JOIN INFOPROD C ON (B.CODIGO_PRODUTO = C.CODIGO_PRODUTO) WHERE a.descricao =''"; but in the case the line that is within {...} is precisely what should return within the inputs and the problem would be solved

  • @Victort. ae complica, better open another question, because the way it is I can’t even know where the problem is, aiming that this subject is already totally other.

Show 16 more comments

Browser other questions tagged

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