0

http://thpsblog.000webhostapp.com/aaDirectorio/index.html <- link to the index.html, it's in Spanish.

I'm following a tutorial on how to set up a Live Search Result because I'm trying to build a directory of "groups", it worked fine before I tried connecting to the database, so the jQuery I've tested and it has worked, it displays text when you type in more than 2 words, but when I tried to search for actual data inside this database, I couldn't get no results shown. I am using my code inside a file named 'backend' like:

backend.php

<?php
include 'db.inc.php';

if(isset($_POST['search'])){
    $search = $_POST['search'];
    $search = "%$search%";

    if(strlen($search) > 2) {
        $sql = "SELECT * FROM directorio_grupos_aa_sonora_sur WHERE grupo LIKE :s || ciudad LIKE :s || reuniones LIKE :s || direccion LIKE :s";

        $stmt = $db->prepare($sql);
        $stmt->bindParam('s',$search);
        $stmt->excecute();

        while($row = $stmt->fetch()){
            $grupo = $row['grupo'];
            $horario = $row['horario'];
            $reuniones = $row['reuniones'];
            $rsg = $row['rsg'];
            $periodo = $row['periodo'];
            $email = $row['email'];
            $telefono = $row['telefono'];
            $direccion = $row['direccion'];
            $ciudad = $row['ciudad'];

            echo "<div>$grupo $reuniones</p>$horario<br>$direccion<br>$ciudad<br>$rsg $telefono $periodo<br>
            <a href='mailto;$email>$email</a></div>";
        }
    }
}
?>

while the 'db.inc.php' handles the connection to the database like:

<?php
try{
    $db = new pdo('mysql:host=localhost;dbname=id9021430_directorio42;charset=utf8','id9021430_directorioaa', 'serviresvivir');}
    catch(PDOException $e){die($e->getMessage());}
?>

and inside the index.html file, at least for the input side:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>

            $(document).ready(function() {
              $("#search").keyup(function(){
                $.ajax({
                  url: 'backend.php',
                  type: 'post',
                  data: {search: $(this).val()},
                  success:function(result){
                    $("#result").html(result);
                  }
                });
              });
            });

            </script>

<section class="container">

  <div class="row">
<div class="col-lg-5">

  <h2>Quick search</h2>
<p>Find groups: </p>
  <input type="text" id="search" />
  </p>
  <span id="result"></span>

</div>
</div>

</section>

So, where could the problem be?

Shadow
  • 32,277
  • 10
  • 49
  • 61
Eduardo
  • 254
  • 1
  • 9
  • Welcome to Stack Overflow! Thanks for providing enough info to look at your question, unusual for a first timer! One note, there is a typo where you setup the **a** tag. You have a ";" after mailto that should be a ":". Also, in SQL you use *OR* instead of *||*. Lastly, you can use phpAdmin to test your SQL statements to be sure you don't have a typo. – Sloan Thrasher May 06 '19 at 21:37
  • @Shadow: Not sure this is a duplicate, although dealing with typos in a SQL statement has been covered in many questions. – Sloan Thrasher May 06 '19 at 21:39
  • @SloanThrasher || is a valid operator in mysql – Shadow May 06 '19 at 21:39
  • @Shadow: Thanks for the info. – Sloan Thrasher May 06 '19 at 21:50
  • @SloanThrasher so i can also use something similar to this : $sql = 'SELECT table1, table2, table3'; ? all in one short single sentence? – Eduardo May 06 '19 at 22:23
  • Nope. Your syntax looks OK, so take a look at the link @Shadow added in the duplicate question note. The problem is that you can't have multiple **:s** in your query string. You would need to use ```:s1 :s2 :s3``` and bind each symbol to the same variable. – Sloan Thrasher May 06 '19 at 22:37
  • that didn't worked, i am using a php variable within the sql code piece bit so then i bindparam that info into the php again to display the content but somehow its not working.... :s – Eduardo May 09 '19 at 21:45

0 Answers0