Query to PHP database with WHERE does not return value

Asked

Viewed 121 times

0

The query variable that I use to query the position in the database is the $fornecedor who receives $_POST['fornecedor'] from another page, but the query does not return values only goes blank, when I use the function mysqli_errno() she returns 0. The connection to the database is configured in the function infoConnect() which is being imported into include, the connection is being made normally. When I do the query without WHERE in the query only using SELECT and FROM with return but this WHERE filter is necessary for the correct functioning of the system, anyway, I am not able to visualize the error.

<?php

    include "../requeriments/connect_info.php";

    $fornecedor = $_POST['fornecedor'];
    echo("<br>");
    echo($fornecedor); /*Teste para ver se variável está definida*/
        
    $query = " SELECT id, nome, tipo, capacidade, custo, telefone, urlthumb FROM fornecedores WHERE nome = '$fornecedor' ";
    $result = mysqli_query(infoConnect(), $query) or die("Erro na consulta.");

    while($row_result = mysqli_fetch_assoc($result)) {

    
?>

    <form method="POST" action="alter_provider.php" enctype="multipart/form-data">
        
    <label for="id" id="formColum_1">Id:</label>
    <input type="number" placeholder="<?php echo(($row_result['id'])); ?>" id="formColumn_2" name="id" disabled><br>

    <label for="nome" id="formColum_1">Fornecedor:</label>
    <input type="text" placeholder="<?php echo(utf8_encode($row_result['nome'])); ?>" id="formColumn_2" name="nome" ><br>

    <label for="tipo" id="formColum_1">Tipo:</label>
    <input type="text" placeholder="<?php echo(utf8_encode($row_result['tipo'])); ?>" id="formColumn_2" name="tipo" ><br>

    <label for="capacidade" id="formColum_1">Capacidade:</label>
    <input type="number" placeholder="<?php echo($row_result['capacidade']); ?>" id="formColumn_2" name="capacidade" ><br>

    <label for="custo" id="formColum_1">Custo:</label>
    <input type="number" placeholder="<?php echo($row_result['custo']); ?>" id="formColumn_2" name="custo" ><br>

    <label for="telefone" id="formColum_1">Telefone:</label>
    <input type="tel" placeholder="<?php echo($row_result['telefone']); ?>" id="formColumn_2" name="telefone" ><br>

    <label for="urlthumb" id="formColum_1">Imagem:</label>
    <input type="file" id="formColumn_2" name="urlthumb" accept="image/jpeg, image/png"><br><br>
            
    <button type="submit" class="btn btn-primary">Alterar</button>


    </form>

<?php  } ?>

2 answers

1


Without having the DB creation script and the data no response can be 100% acerctive, but since the script works without the where and with the where has no return and also no errors, most likely the query ran successfully but there was even no result found by DB.

Looking at your query, you are looking for a vendor with the name $fornecedor. It should be necessary to modify the query to interpolate text:

$query = "SELECT id, nome, tipo, capacidade, custo, telefone, urlthumb FROM fornecedores WHERE nome = '". $fornecedor ."'";

Still, as your filter is by a column called nome most likely to be of the "text" type. In this case it may be problems with DB/table/column Casing or collation. Confirm that the name in the database is exactly the same as the name received in the post (same uppercase and lower case) and change the where to use LIKE in place of =.

(...) FROM fornecedores WHERE nome LIKE '". $fornecedor ."'";

Finally, I would like to add that this type of query should NEVER be made, as it allows anyone to have access to the database through SQL Injection. However this would already be another theme.

0

Since the return happens without the where and its variable is passing the value, what I can think is in the form that was inserted the variable in sql, to test the background give a echo($query) and see how the value is being inserted into your sql. Test concatenate your variable like this.

$query = "SELECT id, nome, tipo, capacidade, custo, telefone, urlthumb FROM fornecedores WHERE nome = '".$fornecedor."'";

Or

$query = "SELECT id, nome, tipo, capacidade, custo, telefone, urlthumb FROM fornecedores WHERE nome = '{$fornecedor}' ";

Browser other questions tagged

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