user1592953
user1592953

Reputation: 135

Spaces returning all results in table

I am having a pretty big problem. I am allowing users to search for other users by interest. The problem is, if a user types in any more than one space, it returns every user (it even duplicates some). What can i do to stop this?

Here is my code, well all that you need to see what is going on:

    $connect= mysqli_connect('localhost', '', '', 'shar31t');

    if (isset($_GET['interest']) && $_GET['interest'] != " ") {
        $interest= rtrim($_GET['interest']);
        $interest= mysqli_real_escape_string($connect, $interest);
        $query= "SELECT user_id FROM interests WHERE interest LIKE 
            '%".$interest."%'";
        $result= mysqli_query($connect, $query);
            }

Thanks

Matt

Upvotes: 0

Views: 40

Answers (2)

Teena Thomas
Teena Thomas

Reputation: 5239

Along with Nelson's answer, do a SELECT distinct user_id to avoid duplicates, as it seems a user can have multiple interests.

Upvotes: 2

You should replace your following line:

if (isset($_GET['interest']) && $_GET['interest'] != " ") {

for this one:

if (isset($_GET['interest']) && trim($_GET['interest']) != '') {

that should prevent an empty string to get to your query.

Upvotes: 2

Related Questions