Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Php/SQL Live search bar not working

edited February 19 in Other Discussions

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: https://pastebin.com/RgHmdUMH
fetch.php:

Thanks!

«1

Comments

  • 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
  • Do anyone have an idea?
  • edited February 22

    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/

  • edited February 22

    @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
  • @Freeranger said:
    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

    If I'm reading between the lines in the official documentation, the statement within the if structure must be on the next line.

    So this doesn't work:

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

    But this does:

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

    This should work as well:

    if(mysqli_num_rows($result) > 0) { array_unique($data); }
    

    And, for the sake of completion, this is the only right format dictated by the PSR-2 coding style guide:

    if (mysqli_num_rows($result) > 0) {
        array_unique($data);
    }
    

    And remember that while Java and PHP are both inspired by C, their syntax is not the same.

  • Thanks

    I just got a big problem - everything stopped working!
    Nos, it Will just display all products.

    Can you see if you can find whats wrong?
    https://pastebin.com/ybhWywW3

  • edited February 25

    Edit: it gives parse error

    Parse error: syntax error, unexpected '$html' (T_VARIABLE) in /home/vol11_3/epizy.com/epiz_21490173/htdocs/test_update/fetch.php on line 40

  • edited February 25

    I'll suggest you to do this
    download and install "PhpStorm" 30 days eval.

    open new proj. > new PHP file
    and paste your code

    in the right upper corner you will have red (!)
    it will say 2 errors, 6 warnings, 2 typos

    or use some other IDE (you mentioned java so you probably have Eclipse or netBeans)
    use/run debugger + step by step, etc. every time you have some problem

  • Thanks!

    I figured out there is an unclosed while loop, But when I'm closing it, it says

    Parse error: syntax error, unexpected ' ' (T_STRING) in /home/vol11_3/epizy.com/epiz_21490173/htdocs/test_update/fetch.php on line 29

    Otherwise it says unexpected end of file.

    Any idea?

  • please use phpStorm !

    I don't have access to fetch.php code.

    but when I last saw you had an extra } x2

  • as I say use phpStorm and at least check syntax (red || on right when some error + mouseover will show more ~~~~ , etc.)
    line 22 and 40 del }

  • Thanks I Will try it
  • BTW: Admin, can you see the first version of My first post?
  • Fixed that error! Can someone go to oilspec.ml/test_update and try to search for one of The products?
    Thanks!

This discussion has been closed.