0
<?php
if(isset($_POST['query'])){
$query = $_POST['query'];
}

//create connection
$conn = mysql_connect("localhost", "root", "password");
mysql_select_db("bookclub", $conn);

?>
<html>

       <head>
          <meta charset="UTF-8">
          <link rel="stylesheet" type="text/css" href="style.css">
          <title> SearchBar </title>                      
        </head>            
              <body>
                 <header>


                    <form action="index.php" method="POST">

                    <input type="text"  name="query" placeholder="search...." maxlength="30" id="search">
                    <input type="submit"  name="submit" id="searchbtn" value="GO" />

                    </form>

<?php
$query = mysql_query("SELECT * FROM list WHERE BookName LIKE'%".$_POST['query'] ."%' OR Author LIKE '%".$_POST['query'] ."%' OR Price            LIKE'%".$_POST['query']."%'");  //line 53
$num_rows = mysql_num_rows($query);            
while($row = mysql_fetch_array($query)){

    $BookName = $row['BookName'];
    $Author = $row['Author'];
    $Price = $row['Price'];

    echo '<h3>'.$BookName.' By </h3> <p>' . $Author.'</p> Price: '  . $Price.'<br />'  ;
}
?>

                  </header>

              </body>            

Notice: Undefined index: query in C:\xampp\htdocs\index.php on line 53

Tried many alternatives but did not get any solution for this.Even though there is an error I am able to search the contents in my db tables.I want this error to be removed.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 05 '17 at 10:15
  • Basically `$_POST['query']` will not exist until the user submits the form. When you first launch the form it will not exist. Look up the `isset()` PHP function – RiggsFolly Jan 05 '17 at 10:19
  • And a bit of inteligent code layout will go a long way to making debugging your code easier. – RiggsFolly Jan 05 '17 at 10:20
  • Ya I m a beginner in php.Is there any solution for this ? – Sai Bhaskar Jan 05 '17 at 10:27
  • Yes read and inwardly digest the Duplicate link at the top of your question – RiggsFolly Jan 05 '17 at 10:31
  • I got this error removed after declaring a variable $query=''; Anyhow thanks – Sai Bhaskar Jan 05 '17 at 10:45

0 Answers0