-1

I have this input box and button in my navigation page

 form class="navbar-form"
 div class="form-group"
 form action="search.php" method="POST">
 input type="text" class="form-control" name="search"  
 placeholder="Search for Products..."
 input type="submit" value="Submit"
 /form
 /div
 /form

which is located at the top of my index.php page and in my search.php $output = '';

<!--collect -->
if(isset($_POST['search'])) {
    $searchQ = $_POST['search'];
    $searchQ = preg_replace("#[^0-9a-z]#i","",$searchQ);
    $query = mysql_query("SELECT * FROM products WHERE title LIKE 
    '%$searchQ%' OR part_number LIKE '%$searchQ%' OR stock_code LIKE 
    '%$searchQ%' OR form_factor LIKE '%$searchQ%' ") or die("could not 
    search!");
    $count = mysqli_num_rows($query);
    if($count == 0) {
        $output = '"There was no search results"';
    } else {
        while($row = mysql_fetch_array($query)) {
            $title      = $row['title'];
            $partNumber = $row['part_number'];  
            $stockCode  = $row['stock_code'];   
            $formFactor = $row['form_factor'];
            $id         = $row['id'];

            $output .= '<div>'.$title.' </div>';
        }
    }
}

when i click the button the url goes from http://localhost/alphacomponents.co.uk/index.php to http://localhost/alphacomponents.co.uk/index.php?search=Asus

and i have dev tool'd it and no errors appear. Any ideas?

Tom Thorpe
  • 49
  • 6
  • You are mixing DB drivers. `mysqli_num_rows` won't work with `mysql_*` update everything to `mysqli`. Also use parameterized queries. – chris85 Sep 20 '16 at 16:49
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – chris85 Sep 20 '16 at 16:50
  • i have changed all mysql to mysqli, but it still doesn't work.the same result still happens?? – Tom Thorpe Sep 21 '16 at 06:26
  • Have you echoed the generated query somewhere and tried it manually to make sure it actually returns what you think it should? – StudioTime Sep 21 '16 at 06:29
  • Update the question with the new code. – chris85 Sep 22 '16 at 21:17

2 Answers2

0

I'd like to comment, but don't have enough reputation.

is the issue because you have a form inside another form? and therefore you are submitting the outer form - which is not the form you want to submit?

You could try below form, and then check that it is navigating to search.php page correctly.

<form class="navbar-form" action="search.php" method="POST">
<div class="form-group">
    <input type="text" class="form-control" name="search" placeholder="Search for Products...">
    <input type="submit" value="Submit">
</div>

laney
  • 339
  • 1
  • 7
0

The main problem is that the url is going from http://localhost/alphacomponents.co.uk/index.php to http://localhost/alphacomponents.co.uk/index.php?search=Asus

It should go to http://localhost/alphacomponents.co.uk/search.php instead.

You need to remove nested form. Put it like:

form class="navbar-form" action="search.php" method="POST"

ankit9j
  • 254
  • 2
  • 7