search is not returning the php, mysql data

Asked

Viewed 33 times

1

I am making a search page so that when I do the search is not returning the data could help me? what I’m missing?

<?php require_once('Connections/conn2.php'); ?>

<?php 


$tipo_prod = $_POST['tipo_prod'];

echo $tipo_prod;

if ($conn1->connect_error) {
    die("Conexão falhou: " . $conn->connect_error);
} 

$result = $conn1->query('SELECT * FROM cad_produtos WHERE tipo_prod = '.$tipo_prod.''); 

if ($result > 0) {                  

    while($row = $result->fetch_assoc()) {
?>



  <section>
  <div class="container py-3">
    <div class="card">
      <div class="row ">
        <div class="col-md-4">
            <img src="upload/<?php echo $variavel_limpa ?>-padrao.jpg" class="w-100">
          </div>
          <div class="col-md-8 px-3">
            <div class="card-block px-3">
              <h4 class="card-title"><?php echo $row['nome_prod']; ?></h4>
              <p class="card-text"><?php echo $row['tipo_prod']; ?></p>
              <p class="card-text"><?php echo $row['desc_prod']; ?></p>
                <!-- Divider -->
        <hr class="my-3">
                <strong>Tamanho (Altura X Largura): </strong><?php echo $row['tamanho1_prod']; ?> X <?php echo $row['tamanho2_prod']; ?>
                <div><strong>Preço do metro²:</strong> R$<?php echo $row['pre_metro2']; ?></div>
                <div><strong>Custo do Produto:</strong> R$<?php echo $row['custo_produto']; ?></div>
               <div><strong>Valor da unidade:</strong> R$<?php echo $row['valor_uni']; ?></div>
                 <!-- Divider -->
        <hr class="my-3">
                <div><strong>Codigo do produto:</strong> <?php echo $row['codigo_prod']; ?></div>

            </div>
          </div>

        </div>
      </div>
        </div>


</section>




<?php }
 } else {
    echo "<center>"."<h1>"."Não há produtos!"."<div>"."<small class="."text-muted".">"."Cadastre um para visualizar!"."</small>"."</div>"."</h1>"."</center>";
 }

$conn1->close();

?>

conn2

<?php
        //cria a conexao mysqli_connect('localizacao BD', 'usuario de acesso', 'senha', 'banco de dados')
        $conn1 = mysqli_connect('localhost', 'root', '', 'gr1');

        //ajusta o charset de comunicação entre a aplicação e o banco de dados


        //verifica a conexão
        if ($conn1->connect_error) {
            die("Falha ao realizar a conexão: " . $conn1->connect_error);
} 

?>
  • $result->num_rows

  • i made the change and this error Trying to get Property of non-object in

  • What mistake it makes?

  • Trying to get Property of non-object in C: xampp 5 htdocs gp1 mostra_prod.php on line 16

  • Enter the Connections/conn2.php code

  • ready this above

  • Var_dump the $result

  • Lonabool(false) Notice : Trying to get Property of non-object in C: xampp 5 htdocs gp1 mostra_prod.php on line 16

  • which field tuple type_prod

  • He’s like, text

Show 5 more comments

1 answer

0


The query is not well built

In the query using the where clause compares with a text type field so you have to involve the comparison value between ', e.g. 'value'

example:

$tipo_prod="tipo1"

$result = $conn1->query("SELECT * FROM cad_produtos WHERE tipo_prod = '$tipo_prod'"); 

I also advise to read about sql Injection

https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php

  • I got it, I didn’t see the simple quotes, but thank you so much for helping me, I had used a similar code to get an id

Browser other questions tagged

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