Search script does not return results or errors

2

Well, I created a PHP search script, but it does not display results, and it also does not display errors.

Fomulário

<form class="navbar-form navbar-right" method="post" action="pesquisa_card.php">
    <div class="form-group">
        <input type="text" class="form-control" placeholder="Pesquisar" name="search-text" maxlength="255">
    </div>
    <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span><span class="sr-only">Buscar</span></button>
</form>

Search_Card.php

<?php
require("components/header.php");
require("php-scripts/conecta.php");
require("php-scripts/banco-cadastros.php");

$busca = $_POST["search-text"];

$buscados = pesquisaCards($conexao, $busca);
foreach ($buscados as $encontrado) {
  ?>
  <div>
    <!-- <?= $card['id'] ?> -->
    <div class="thumbnail">
      <img src="<?= $encontrado['link_image'] ?>" alt="imagem de <?= $encontrado['card_name'] ?>">
      <div class="caption">
        <h3><?= $encontrado['card_name'] ?></h3>
        <p><?= $encontrado['effect_description'] ?></p>
        <p class="btn-holder"><a href="#" class="btn btn-info" role="button">Detalhes</a> <a href="#" class="btn btn-danger" role="button">Excluir</a></p>
      </div>
    </div>
  </div>
  <?php
}

require("components/footer.php");

Search function, pulled by bank-cadasters.php

function pesquisaCards($conexao, $busca){
    $buscados = array();
    $resultado = mysqli_query($conexao, "select C.id, C.card_name, C.monster_lrl, C.link_image, C.effect_description, C.card_atk, C.card_def, C.quantity, E.edition_name, E.edition_code, S.card_status_desc, A.kind_attribute, K.kind_name, M.kind_monster_name from card C inner join card_editions E on E.id = C.id_card_edition inner join kind_attributes A on A.id = C.id_kind_attributes inner join kind_card K on K.id = C.id_kind_card inner join kind_monster M on M.id = C.id_kind_monster inner join card_status S on S.id = C.id_card_status where (('card_name' like '%".$busca."%') or ('effect_description' like '%".$busca."%') or ('edition_name' '%".$busca."%') or ('edition_code' '%".$busca."%') or ('kind_attribute' '%".$busca."%') or ('kind_name' '%".$busca."%') or ('kind_monster_name' '%".$busca."%'))");
    while($encontrado = mysqli_fetch_assoc($resultado)){
        array_push($buscados, $encontrado);
    };
    return $buscados;
};

Connection

$conexao = mysqli_connect("localhost", "root", "", "cardsystem");
  • I was able to correct the first error, which did not return any value, because there were spaces in the query, between the concatenations, which were counted as part of the search. However, it still does not return any values when I look up the records, and if I do not write anything, it returns all the values in the database, not displaying errors
asked by anonymous 07.08.2017 / 16:38

2 answers

3

In addition to what Everaldo you have to put the table prefixes in each field:

$query = 

"SELECT C.id, C.card_name, C.monster_lrl, C.link_image, C.effect_description, 
C.card_atk, C.card_def, C.quantity, E.edition_name, E.edition_code, 
S.card_status_desc, A.kind_attribute, K.kind_name, M.kind_monster_name 
FROM card C 
INNER JOIN card_editions   E on E.id = C.id_card_edition 
INNER JOIN kind_attributes A on A.id = C.id_kind_attributes 
INNER JOIN kind_card       K on K.id = C.id_kind_card 
INNER JOIN kind_monster    M on M.id = C.id_kind_monster 
INNER JOIN card_status     S on S.id = C.id_card_status
WHERE ((C.card_name      like '%".$busca."%') 
OR (C.effect_description like '%".$busca."%') 
OR (E.edition_name       like '%".$busca."%') 
OR (E.edition_code       like '%".$busca."%') 
OR (A.kind_attribute     like '%".$busca."%') 
OR (K.kind_name          like '%".$busca."%') 
OR (M.kind_monster_name  like '%".$busca."%'))";

$resultado = mysqli_query($conexao, $query);

In addition, get used to organize the code that is much easier to notice the errors like the one I did.

    
08.08.2017 / 16:16
6

I noticed that in your SQL has an ERROR in the condition used, it is missing to write "LIKE" in the last conditions:

where 
(('card_name' like '%" . $busca . "%') or 
('effect_description' like '%" . $busca . "%') or 
('edition_name' '%" . $busca . "%') or  <-- Cade o LIKE
('edition_code' '%" . $busca . "%') or <-- Cade o LIKE
('kind_attribute' '%" . $busca . "%') or <-- Cade o LIKE
('kind_name' '%" . $busca . "%') or <-- Cade o LIKE
('kind_monster_name' '%" . $busca . "%'))<-- Cade o LIKE
    
07.08.2017 / 17:10