Jump to content

How to use WHERE and LIKE with a variable


cainam29

Recommended Posts

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;
    if(isset($_POST['uid'])){
        $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);
            $stmt->execute();
            $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.

 

Link to comment
Share on other sites

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

 

Then your problems will go away.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.