It's impossible to know why you're getting this error without more code. That said, the usual reason to get this error is usually because you're using a variable before you've set it. It's really undefined.
$mystring = "Blah blah $subject"; // bad. $subject doesn't exist yet.
$subject = "Hello World!"; // do this first
$mystring = "Blah blah $subject"; // Good, because $subject now has a value
Of course, it could be something else, like it could be because you unset()
it somewhere beforehand, or possibly the code you think is setting it is actually not (don't ask me why without posting more code). In fact, it could be a typo somewhere else in your code. But generally, this usually is the answer. If this doesn't work for you, I would suggest you post a slightly larger snippet of code. That way, we can dig into exactly why you are getting this error and help you better.
Side note: I should also mention (as many people probably will eventually) that your code is also vulnerable to SQL Injection. This can be fixed. I only mention it because a) others will if I don't, and b) this can be a very nasty vulnerability to have. You don't want a breach to happen to you!