Searching a table for like matches based on 2 different vars

42
January 11, 2019, at 2:00 PM

I have a form that submits 2 values to the post target:

$fname=$_POST["fname"];
$lname=$_POST["lname"];

On the target page, I need to search the specified table for matches that are like $fname OR $lname. I'm kind of struggling with the concat part of it.

#    $user_info="SELECT * from player_data WHERE firstname like %'$fname'% or lastname like %'$lname'%";
     $user_info="SELECT * from player_data where '$fname' like concat(firstname, '%') or '$lname' like concat(lastname, '%')";
     $user_data=mysqli_query($con, $user_info);
     while ($row = mysqli_fetch_array($user_data,MYSQLI_BOTH))
     {
         $getfname=$row['firstname'];
         $getlname=$row['lastname'];
         echo "$getfname $getlname<br>";
     }

that first line that's commented, was something I was trying, figured should try to use concat instead.

Answer 1

I actually figured it out... %$var% works fine, but if one of the 2 were blank it was returning everything in the table. I appreciate the feedback and will definitely look into preventing sql injection.

READ ALSO
Ordering by the order of values in a SQL IN() clause

Ordering by the order of values in a SQL IN() clause

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause

31
Why my function is not returning a value using another function as input?

Why my function is not returning a value using another function as input?

I have written two functionsThe first one utilized a select query and returns a value

10
Display results from multiple groupings in one query

Display results from multiple groupings in one query

I have a queries similar to below:

25
How do query and update MySQL hosted on AWS RDS from POSTMAN before sending a post request [on hold]

How do query and update MySQL hosted on AWS RDS from POSTMAN before sending a post request [on hold]

I am trying to query Database to check if the user has a certain restriction before sending a Create/Post request from Postman

31