Php/SQL Live search bar not working

Hi, I’m coding a search bar for a project, but it doesn’t really work. If I have a product in my database called product e, and write e in the database, the product will, of course, show up. But if I then write e+htyjfthtyhrf, it will still display that product, but it should display no matches! Do anyone know how I can fix this?

index.php: index.php - Pastebin.com
fetch.php:

Thanks!

Admin, please remove fetch.php link!

I’ve checked your site but I don’t see any results regardless of the query input.

It’s att oilspec.ml/test_update

All of this code is located here.

*The page is located there

Ah, I was looking at the search on your home page, thank you for submitting the right URL.

I checked a complicated query like the one in your original post. However, I saw this error in the response:

<br />
<b>Warning</b>:  array_unique() expects parameter 1 to be array, string given in <b>/home/vol11_3/epizy.com/epiz_21490173/htdocs/test_update/fetch.php</b> on line <b>45</b><br />

Maybe it’s unrelated, but it wouldn’t hurt to address it.

Thanks!

It’s unrelated, but I’ll still try to fix it. But do you r abything else, that may cause the problem I have?

Thanks

*See

Do anyone have an idea?

I would recommend to start with classic search button and form

<form role="search" method="get"><input type="text" name="q" placeholder="Type search text and hit enter"></form>

and then put on that button and label some listeners (if clicked, mousedown, enter key pressed, etc.)

then grab the user input from search label
parse this input (toLowerCase, no white space, no hack stuff, chars limit, minimum char lenght for search, etc.)

then search db

and then output result on some <div> or similar

and then auto-clear search label

and when it works continue to polish the code

otherwise you depend on someone big scripts
and it’s not easy to understand how/what that js works and how they are implemented

it also depends if some class can access data/function from another class
whether DOM is ready or not, when js is executed…

in short you need to simplify it and then track how some VALUE is carried…
watch if there is some “NULL” or “exception” as result

probably not related

I checked bootstrap3-typeahead.min.js
and find .bind which is obsolete and is not compatible with jquery v3
The simplest solution is to edit the script and change it with the appropriate one .on
and serve on your server

https://api.jquery.com/bind/
https://api.jquery.com/category/deprecated/deprecated-3.0/

@Freeranger said:
Thanks!

It’s unrelated, but I’ll still try to fix it. But do you r abything else, that may cause the problem I have?

Thanks

It’s not unrelated. I checked your code and it explains the issue. This is what happens:

  1. The long search string means the query results in zero rows.
  2. The $data variable which is supposed to contain result rows is set to No Data Found.
  3. You try to do array_unique($data) to filter duplicate rows, but this crashes because $data is no longer an array.
  4. The PHP script crashes, meaning the request returns with a status code of 500.
  5. The jQuery script won’t call the handler, because it’s a success handler and the AJAX call did not complete successfully.

In other words: fix the array_unique problem and your code works.

Ok thanks,

But I’m Kinda new to this, and I have No idea about how to fix the array_unique thing. Do you know how to do it?
Thanks

@Freeranger said:
Ok thanks,

But I’m Kinda new to this, and I have No idea about how to fix the array_unique thing. Do you know how to do it?
Thanks

You need to rewrite your code so the array_unique line is only hit if there actually are any results. Or you need to make sure that $data always contains an array. There are many, many different ways to work around this.

Thanks!
I tried by using this code:

if(mysqli_num_rows($result) > 0) array_unique($data)

But it returns an error.
I can’t find anything weird with that code(AT last it would work in java).

Know why that happens?
Thanks

http://php.net/manual/en/function.empty.php

if array is empty

    if (!empty($result) {
                                             // do something if array is not empty
    }else{
                                             // do something if is empty
    }

array is null or not

 if (is_null($result) {
                                             // do something if is null
    }else{
                                             // do something if is not null
}

@Freeranger said:
Thanks!
I tried by using this code:

if(mysqli_num_rows($result) > 0) array_unique($data)

But it returns an error.
I can’t find anything weird with that code(AT last it would work in java).

Know why that happens?
Thanks

So… what does the error say?

Parse error: syntax error, unexpected ‘array_unique’ (T_STRING) in /home/vol11_3/epizy.com/epiz_21490173/htdocs/test_update/fetch.php on line 45