-2

I am trying to create search menu in my website, i am using php and mysqli ,i have created all the credential stuffs which are all required to it but still it showing me some errors,can anyone help me from this. analyze.php

<!DOCTYPE html>
<html>
  <head>
    <title>Gtec NetWork</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="asset/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="#">GTEC Network</a>
            <ul class="nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">Boys</a></li>
                <li><a href="#">Girls</a></li>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row">
            <div class="span12">
                    <form method="POST" action="search.php">
                    <label>Search
                            <input type="text" name="keywords" >
                            <input type="submit" class="btn btn-primary lg" value="Search">
                            </label>

                    </form>
            </div>
        </div>
    </div>
  </body>
 </html>

search.php

<?php  
    require_once 'lib/db.php';
?><?php
    if (isset($_GET['keywords'])) {
        $keywords = $db->escape_string($_GET['keywords']);
        $query = $db->query("
            SELECT *
            FROM user 
            WHERE  registernumber LIKE '%{$keywords}%'
            OR username LIKE '%{$keywords}%' 
            "
            );
        $run = mysqli_query($query);

    }

?>
    <!DOCTYPE html>
<html>
  <head>
    <title>Gtec NetWork</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="asset/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="#">GTEC Network</a>
            <ul class="nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">Boys</a></li>
                <li><a href="#">Girls</a></li>
            </ul>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-4">
                Found<?php echo $query->num_rows;?> results.
                <?php  
                    if ($query->num_rows) {
                        while($r = $query->mysqli_fetch_array())
                        {
                        ?>

                        <div class="gylphicon glyphicon-heart">
                            <a href="#"><?php echo $r->username;?></a>
                        </div>
                        <?php   }
                    }?>

            </div>
        </div>
    </div>
    </body>
</html>

The problem is

Found Notice: Undefined variable: query in C:\xampp\htdocs\php\search.php on line 39

Notice: Trying to get property of non-object in C:\xampp\htdocs\php\search.php on line 39 results. Notice: Undefined variable: query in C:\xampp\htdocs\php\search.php on line 41

Notice: Trying to get property of non-object in C:\xampp\htdocs\php\search.php on line 41

Please help me

2
  • In your html code method is "POST" and in your php code you search for GET var ? Change the method in your html form to GET
    – e-Learner
    Jul 4, 2015 at 1:00
  • Why does this line appear in the code? $run = mysqli_query($query); ... What is that supposed to do? What the plastic? Jul 4, 2015 at 1:06

2 Answers 2

0

$query is declared inside the if-block

if (isset($_GET['keywords'])) { ... }

So, if $_GET['keywords'] isn't set, $query won't be defined later in the script.

Maybe you can do something like this :

if (isset($_POST['keywords'])) {
    $keywords = $db->escape_string($_POST['keywords']);
    $query = $db->query("
        SELECT *
        FROM user 
        WHERE  registernumber LIKE '%{$keywords}%'
        OR username LIKE '%{$keywords}%' 
        "
        );

} else {
    $query = $db->query("
        SELECT *
        FROM user
        "
        );
}
8
  • So what to do for that? Jul 4, 2015 at 0:50
  • Do not use $query in your body if $_GET['keywords'] isn't defined.
    – dlegall
    Jul 4, 2015 at 0:51
  • Then where i query from database Jul 4, 2015 at 0:52
  • You're not querying anything if $_GET['keywords'] isn't defined, see your own code. So, if you're not querying anything, you can't display any result, right?
    – dlegall
    Jul 4, 2015 at 0:53
  • I am asking about where i can put select * user ,mysql query? Jul 4, 2015 at 0:55
0

As already pointed out by @dlegall, what is immediatlely sure is: the errors you're getting come from the fact that keywords is currently not set.

From there, two points:

  1. Anyway your code must prevent this case, so you cannot simply use any $query->... in the bottom (HTML) part without surveying it.
    So you might use something like:

    <div class="col-md-4">
      <?php if (@$query) { ?>
        Found<?php echo $query->num_rows;?> results.
        <?php  
            if ($query->num_rows) {
                while($r = $query->mysqli_fetch_array())
                {
                ?>
                <div class="gylphicon glyphicon-heart">
                    <a href="#"><?php echo $r->username;?></a>
                </div>
                <?php   }
            }?>
      <?php } else { ?>
        No keywords available.
      <?php } ?>
    </div>
    
  2. This way your application is now capable to respond to any situation.
    Now you may test for what is currently causing keywords to be empty: this may be a totally different issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.