How to make a mysql query from a link

Asked

Viewed 638 times

-1

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

Assuming the link within the tag to were it so:

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

The user clicks on the link, the query is made in the database 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.";
}
  • Welcome to Stackoverflow in English, which you have tried to do?

1 answer

0

You can achieve this by passing a parameter to the page, then the PHP can take this parameter and generate the appropriate content for the reader. For example, by clicking 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 are asking, this is an example of how it could work for you, test there and see if it helps you.

  • Paul, but how would the parameter passed to the page?

  • @valpmg is already in the example code, the parameter is precisely the "shirts", it will click on the link that in turn sent to the server a post with the click and the data and will return the query, on the same page or another according to the expected behavior of the structure of your site.

  • It didn’t work here. the link should be inside a post form?

  • 2

    @valpmg In this situation one should use $_GET. And still put on the link href="seulink.php?camisas". So he will call the if(isset($_GET['camisas'])). Now if you want to pass by $_POST. Ai should use javascript.

  • Willian was exactly what I was in doubt about. Thanks for the hug

Browser other questions tagged

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