How to do a mysql query from a link

-1

I have seen in some sites links that make mysql searches and return results, ie, make a mysql query from a link instead of a field.

Assuming the link within the tag to is like this:

href="https://www.meulink.com" name="camisas" >Camisas

The user clicks on the link, the query is done in the database by returning the corresponding values:

$sql = "SELECT roupas FROM estoque WHERE roupas='camisas';
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["roupas"]."<br/>";
    }
} else {
    echo "Nada foi encontrado.";
}
    
asked by anonymous 15.06.2016 / 17:55

1 answer

0

You can achieve this by passing a parameter to the page, then the PHP can pick up this parameter and generate the appropriate content for the reader. For example, when you click on shirts, you can have:

<a href="https://www.meulink.com" name="camisas" >Camisas</a>

BackEnd action:

if(isset($_POST['camisas'])){
//Se a pagina enviar como post um clique nas camisas, então execute mysql query
$sql = "SELECT roupas FROM estoque WHERE roupas='camisas';
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["roupas"]."<br/>";
    }
} else {
    echo "Nada foi encontrado.";
}
}

Of course there are several ways to do what you're asking for, here's an example of how it might work for you, test it out and see if it helps.

    
15.06.2016 / 22:25