-1

I am a beginner at programming. I am learning PHP. What i am trying to do is write a search script in PHP for my project. When i try searching through MySQL database it gives me an error:

Notice: Undefined variable: output in C:\xampp\htdocs\search.php on line 2

I have checked everything on the script and cant see the problem. Have i coded it wrong?

i have checked all possible questions on the forum that relate to my question and they dont seem to gime me the answer i need. please help.

this is the HTML script with the input:

    <form action="search.php" method="post" id="search">
      <div id="searchfield_div">
        <input name="search" id="searchfield" type="text" placeholder="What you looking for?">
        <input id="delete_search_button" name="delete button" type="button" value="X">
        <input id="search_button" name="search button" type="submit" value="Search">
      </div>
    </form>

and this is my PHP script:

<?php
//connection script
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "liquihub";
$output = '';

@mysqli_connect("$db_host","$db_user","$db_pass","$db_name") or die("could     not connect");

//collection script
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","", $searchq);

$query = mysql_query("SELECT * FROM beverage_db WHERE name LIKE '%$searchq%' OR price LIKE '%$searchq%' OR type LIKE '%$searchq%'") or die ("could not search" );
$count = mysql_num_rows($query);
if ($count == 0) {
     $output = 'We not stocking this particular item at present';
}else{
    while($row = mysql_fetch_array($query)) {
        $bevname = $row['name'];
        $bevprice = $row['price'];
        $bevtype = $row['type'];
        $bevid = $row['id'];

        $output .= '<div>'.$bevname.' '.$bevprice.' '.$bevtype.'</div>';
      }
   }
 }
?>

The output script that's meant to put the results on a different page:

<?php print("$output");?>
user2938948
  • 13
  • 1
  • 4
  • You can not mix `mysqli_` and `mysql_` functions. You should stick with `mysqli_` – tkausl Jul 09 '16 at 21:01
  • I have changed them all to mysqli_ but it's still the same problem – user2938948 Jul 09 '16 at 21:22
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – matiaslauriti Jul 09 '16 at 21:27

1 Answers1

0

You have no problem, what you are getting is a Warning not an Error. Your question is duplicated. Here.

Community
  • 1
  • 1
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • I have tried that solution, and still the same thing. What do I need to change to make it output results? Cause there's nothing on the output page exempt that notice – user2938948 Jul 10 '16 at 12:18
  • If you are changing pages, `$output` will not be save, for that use sessions or use include. And dont do `print("$output")` do directly `echo $output` – matiaslauriti Jul 10 '16 at 17:51
  • where would i put sessions? how would i write out my code? – user2938948 Jul 11 '16 at 11:31