Jump to content


How to use WHERE and LIKE with a variable

php sql

  • Please log in to reply
1 reply to this topic

#1 cainam29

  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 25 August 2017 - 09:18 AM

I have a login session where it checks the user name and displays it to the form (it displays FirstName LastName). That username is also being used as a variable to pull up data in db. Now I also have another page where user's can update the db, I dont have a problem if the user will update it with a complete FirstName LastName entry because it will just be the same as the one's being used by the login session, but sometimes they just update it with FirstName.
The problem starts when I have values in TEST db under Tester column and in USERS db under User column that is of different values. It would have two diff values when a user did not enter the full FirstName LastName.
For example,
FirstName1 LastName1 is the value in USERS db User column - this is fixed and is being used in a login session
User updated Tester column in TEST db with just FirstName1 - this is different from the User column above
Here is what I am trying to do,
Getting list of tickets from the TEST db where datefrom and dateto and using a variable for the values that is in User column under USER DB
If User column under USERS db = Tester column under Test db which is FirstName1 LastName1 - it will be good as I will be able to get tickets under FirstName1 LastName1.
But I will not be able to get ticket which is still assigned to that same person because the value in Tester column under Test db is just FirstName1.
If User column under USERS db (FirstName1 LastName1) is not equal to Tester column under Test db which is just FirstName1 - I will not get tickets assigned to FirstName1 as my variable is equal to FirstName1 LastName1.
I hope that I explained it clearly, here is my code,
Here is the variable that I am posting,   
$uid = false;
        $uid = $_POST['uid'];
And here is the query,    
$sql = 'SELECT `id`, `date_implemented`, `tester`, `comments` 
            FROM `tracker` WHERE `tester` = :uid AND `scheduled_start_date` BETWEEN :d1 AND :d2';
            $stmt = $conn->prepare($sql);
            $stmt->bindParam(':uid', $uid, PDO::PARAM_STR);
            $stmt->bindParam(':d1', $date['from'], PDO::PARAM_STR);
            $stmt->bindParam(':d2', $date['to'], PDO::PARAM_STR);
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
I would need help in passing that variable (uid) such that I can use `WHERE tester LIKE uid`. In that case whether the tester column just contains FIRSTNAME1 or a complete FIRSTNAME1 LASTNAME1, I would be able to get all tickets assigned to FIRSTNAME1.

#2 requinix

  • Administrators
  • Maddening Administrator
  • 9,719 posts
  • LocationWA

Posted 25 August 2017 - 09:30 AM

1. Don't reference people by names in the database. Use IDs instead.
2. Don't let users enter just a name for a person who is also a user in your system. Make them choose the user in such a way that you always get the user ID. You can use a <select> or <datalist> if the list of people isn't very long, or use autocomplete if it is.

Then your problems will go away.
The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users