How to fetch the product by its title?

Asked

Viewed 36 times

0

I am trying to search for the title of the product, but regardless of the title consulted, it always returns me the last record of the Product table.

I am using the following code below to run the search:

<?php
include "conexao.php";
$buscar = $_GET['buscar']; 
$sql=$pdo->prepare("SELECT * FROM produto WHERE titulo LIKE '%".$buscar."%'");
$sql->execute();
foreach($sql->fetchAll() as $res){

echo'
<div id="prod" style="background-color:'.$res["fundosite_cor"].';width:33%; float:left; padding:10px 0;" class="center_prod_box">
    <div align="center" id="titulo" style="width:100%;">
        '.$res["titulo"].'
    </div>
    <div align="center" style="width:100%; height:130px; background-color:'.$res["fundosite_cor"].';">
        <div align="center">
            <a href="prod_detalhe_5.php?codigo='.$res["codigo"].'">
                <img style="width:100%; max-width:100px;" src="img_produtos/'.$res["img01"].'" />
            </a>
        </div>
    </div>
    <div align="center" id="preco" style="width:100%;">
        <span style="">R$ '.$res["preco"].'</span>
    </div>                        
    <div align="center" id="carrinho" style="width:100%;">
        <a href="prod_carrinho.php?acao=add&codigo='.$res["codigo"].'">
            <img style="width:100%; max-width:20px;" src="img/carrinho.png" title="Por no Carrinho" />
        </a>
    </div>                        
 </div>
';
}?>

I would like to know how to get him to return the product related to consultation with Titulo.

  • Using HELP or HELP on issue titles is completely unnecessary, please avoid in future questions. Recommended reading: [Ask]

  • The idea of using Pdo and Prepared statements is precisely not having to interpolate values in the string that represents the query. So it should be LIKE ? and then the execute receive the value that comes from $buscar, or even with bindParam.

  • I don’t understand Isac, I’m half layman, could you give me an example of how I should do?

  • http://php.net/manual/en/pdo.prepare.php

No answers

Browser other questions tagged

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