Search script does not return results or errors

Asked

Viewed 78 times

2

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

Phomularius

<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 the database.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, in which no value returned, 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 value when searching for the records, and if I do not write anything, it returns all the values of the database, showing no errors
  • probably the error this in the query also checks if you have data corresponding to that query

  • I checked, I have all the data, so much that I played in mysql and was not pointed out any error, but also returned no record

  • 1

    try adding records and see if it works in php

  • It is good always before you put SQL code to run through the put application to run in the database directly to check whether it returns data or syntax errors.

  • @Claudioramosdeoliveira, already realized this, but it does not return errors, and also does not return values

  • @13dev as add records ?

  • @Murilogambôa add records in mysql to have inputs in php

  • there are records, when entering the query in mysql, I search for "Kaiju", and I have 8 different "Kaiju" records

Show 3 more comments

2 answers

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
  • added the Like’s and still not running

3


Beyond what I said 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);

Also get used to organizing the code that is much easier to notice the mistakes like the one I made.

Browser other questions tagged

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