PHP Search error

Asked

Viewed 58 times

1

I do the search with the existing numMec and appears to me that does not exist in the database

<?php
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9]#i","",$searchq);
$query = mysql_query("SELECT * FROM bombeiros WHERE numMec = '%$searchq%'") or die("Nao consegue pesquisar");
$count = mysql_num_rows($query);
if($count==0){
    $output='<div class="alert alert-warning alert-dismissable fade-in">
                <a class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <strong>Bombeiro</strong> não existe na base de dados.
            </div>';
}else{
    $nome=$row_bombs['Nome'];
    $num=$row_bombs['numMec'];

    while($row = mysql_fetch_array($query)){
        $output='<div class="col-sm mx-auto" style="padding-top: 20px;">
                    <div class="card" style="width: 300px;">
                        <img class="mx-auto card-img-top" src="img/BVMCN.png" style="width: 50%; padding-top: 10px;"></br>
                        <div class="card-body">
                            <h5 class="card-title"> '.$nome.'</h5>
                            <a href="bombeiro.php?id="'.$num.' class="btn btn-primary col-sm">'.$num.'</a>
                        </div>
                    </div>
                </div>';
    }
}
}
?>
<?php print("$output");?>

and this at the end of me an error(Notice: Undefined variable: output)

  • 3

    Because you put numMec = '%$searchq%'", instead of equal would not be like? Probably your query is wrong.

  • but I want it to be the same result and not like

  • because if I put like does not appear the number I searched but another

  • What comes in $_POST['search'] ? What kind of numMec on the table bombeiros ?

  • are numbers in all 8 and this as int

  • So that the symbol of percentage will not be searched using the like? In doing numMec = '%$searchq%', you’re making sql understand numMec = '%12345678%', so it won’t bring any results. Or remove the % or use the like instead of =

Show 1 more comment

2 answers

0


At the beginning of your code add it like this: $output = '';

The error that had appeared was notifying that its variable did not exist because it was only set in the if(){} so that’s why I asked you to write it at the beginning with the empty value or null, now regarding your research is quite simple your $query is wrong look at the example below.

Current:

$query = mysql_query("SELECT * FROM bombeiros WHERE numMec = '%$searchq%'") or die("Nao consegue pesquisar");

Update by:

$query = mysql_query("SELECT * FROM bombeiros WHERE numMec = nome da coluna LIKE '%$searchq%'") or die("Nao consegue pesquisar");

Test that will now work.

  • Says there’s no such thing as what I’ve been looking for

  • I have 5 at the moment

  • try as follows: $query = mysql_query("SELECT * FROM firefighters where numMec = '$searchq'") or die("Can’t search");

  • So one comes along but not what I researched

  • Try it this way: $query = mysql_query("SELECT * FROM firefighters WHERE numMec like ='$searchq' ') or die("Can’t search");

  • https://i.stack.Imgur.com/FIO0F.png

  • Can’t search with LIKE =

  • Write it down when you get home I’ll answer you better.

  • I’ve already solved

  • How did your code turn out?

  • I had to change another code before that was what caused the conflict

  • Show the important that managed to make it work.

Show 7 more comments

-2

At the beginning of your code add it like this: $output = '';

Browser other questions tagged

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