I can’t run a query with PHP and SQL

Asked

Viewed 62 times

2

good night

I’m new to php, and I’m having trouble making the search field work. I’m making a system that has a tutorial page, which has the cards with the tutorial information and a search field. I made a loop to display the information and their respective cards and a query, and it all worked out. However, when I add the search query, absolutely nothing happens - I put it to return the tutorial name in a div, just to test. I don’t know what’s going on, if it’s some logic or syntax error. Can anyone help me? Note: I changed the position of the search code several times, this last attempt was the most desperate ksks

    <?php
require_once "config.php";
?>

<!DOCTYPE html>

<html>

<head>

    <title>Tutoriais</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <script type="text/javascript" src="js/script.js"> </script>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" type="text/css" href="css/estiloTutoriais2.css"/> 

</head>

<body>

<div id="mySidenav" class="sidenav">
        <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
        <a href="index.php">Home</a>
        <a href="tutoriais.php">Tutoriais</a>
        <a href="forum.php">Forum</a>
        <a href="contato.php">Contato</a>
    </div>

<ul id="listaH"> 
 <li> <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; OPUS</span></li>
</ul>

<section>

 <div class="images">
    <img src="images/tutoriais.png" width="800px" height="500px">
 </div>

 <h1 id="titulo_pag"> Tutoriais </h1> <br> 



  <?php
if($_SERVER["REQUEST_METHOD"] == "POST"){

    $pesquisar = $_POST['pesquisar'];
    $sql_pesq = "SELECT * FROM tutoriais WHERE nome LIKE '%$pesquisar%' ";
    $resultado = mysqli_query($link, $sql_pesq);

    while($rows_t = mysqli_fetch_array($resultado)){

        ?>
          <div>  <?php echo "Nome do tutorial: ".$rows_t['nome']."<br>";  ?> </div>
        <?php
    }
    mysqli_close($link); 
}

?>

<input type="text" name="pesquisar" placeholder="Pesquisar Tutorial"> 
  <i class="fa fa-search" aria-hidden="true"></i> <br> <br>
  <input type="submit" value="ENVIAR">


<?php
$sql = "SELECT * from tutoriais";
$result = $link -> query($sql) or die ($link -> error );
if($result = mysqli_query($link, $sql)){
?>
 <div class="container">
<?php 
while ($row = mysqli_fetch_array($result)){
?>

        <div class="card">
            <div class="face face1">
                <div class="content">
                <img src="images/<?php echo $row["imagem"];?>">
                <h3><?php echo $row["nome"];?></h3>
                </div>
            </div>
            <div class="face face2">
                <div class="content">
                    <p> <b> <?php echo $row["titulo"];?> </b> </p>
                    <p><?php echo $row["descricao"];?> </p>
                        <a href="<?php echo $row["link"];?>" target="_blank">Mais</a>
                </div>
            </div>
</div>
     <?php } ?>
  </div>
  <?php 
  mysqli_free_result($result);
}
else{
    echo "Não foram encontrados registros que correspondam a query.";
} 

mysqli_close($link);

?>



<script type="text/javascript" src="js/script.js"> </script>

</section>

</body>

</html>
  • 1

    You tried to run the query directly in the database to see if any data is returned ?

  • I just executed, and it’s working normally (in the bank). When I had not yet created the other query to display the cards, I had used the same code and was working

  • try a var_dump($result) to check if the data is coming.

  • So at first glance, not knowing what’s in . js, what I see missing is a tag <form> around the input / Ubmit.

  • If the problem is the if condition is not being met, I suggest you test if the search field is not empty, then do the search.

  • Guys, that’s right. I was forgetting the form, in which I insert the post method. My God, I don’t know how I didn’t notice before. Anyway, thank you very much!!

Show 1 more comment
No answers

Browser other questions tagged

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