UNDEFINED variable problem


#1

i have this script that i made that shows the status of all repairs for game systems that are currently being done. The status listing page for ALL repairs works great, however, the search page (which searchs for last names) always returns an undefined index notice which doesn’t make sense because the search form works fine too.

Notice: Undefined index: searchstr in C:Websitesvdospecialtiesresults.php on line 9

Line 9:

$searchstr == $_POST['searchstr'];

my sql query:

$query = "SELECT * FROM results WHERE lastName LIKE '%$searchstr%'";

Does anyone know what is wrong?


#2

Looks like the double equals sign should be a single
== is comparison operator = is assignment operator

[php]
// Assigns $_POST[‘searchstr’] value to $searchstr
$searchstr = $_POST[‘searchstr’];

// Compares $_POST[‘searchstr’] value with $searchstr
$searchstr == $_POST[‘searchstr’];

[/php]


#3

even with one equal sign, it still gives me the error. with one equal sign in fact, the sql query returns every entry in the database, not just the ones i am searching for.

here is the entire script:

edit: (just changed the script to support lastname and first name, same errors result and it wont work without == again)

[php]
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>

Search Results

Status Page | Add Record

<? @$lastName == !empty($_POST['lastName']) ? $_POST['lastName'] : '' ; @$firstName == !empty($_POST['firstName']) ? $_POST['firstName'] : '' ; $conn = mysql_connect(works but cannot reveal); mysql_select_db("status", $conn); if(!empty($_POST['firstName'])){ $query = "SELECT * FROM results WHERE lastName LIKE '%$lastName%'"; } else { $query = "SELECT * FROM results WHERE lastName LIKE '%$lastName%' AND firstName LIKE '%$firstName'"; } $result = mysql_query($query, $conn); print "n"; print "n"; while ($field = mysql_fetch_field($result)){ print " n"; } print "nn"; while ($row = mysql_fetch_assoc($result)){ print "n"; foreach ($row as $col=>$val){ print " n"; } print "nn"; } print "
$field->name
$val
n"; ?> [/php]

#4

Your use of
[php]if(!empty($_POST[‘firstName’])){ [/php]
is not correct.

First of all I would use the locally assigned variable instead of the superglobal (only becuase I find it a pain to keep typing the format for the Superglobal array, and it cannot be included directly in a quoted statement).

Additionally, you are saying if $_POST[‘firstName’] is NOT empty then execute the first section which DOES NOT include firstname, else execute the second section which DOES include firstname.

The first part of the if is not the problem, If $_POST[‘firstName’] is NOT empty (thus it does have data) then you don’t use it’s value in the execution of the first query. if $_POST[‘firstName’] IS empty (No Data) then you execute the second query which is looking for data that doesn’t exist.

So you can either leave it like you have and switch the queries around or you can change your IF statement. Below is how I would do it.

[php]
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>

Search Results

Status Page | Add Record

<? $lastName == !empty($_POST['lastName']) ? $_POST['lastName'] : '' ; $firstName == !empty($_POST['firstName']) ? $_POST['firstName'] : '' ; $conn = mysql_connect(works but cannot reveal); mysql_select_db("status", $conn); if($firstName==''){ $query = "SELECT * FROM results WHERE lastName LIKE '%$lastName%'"; } else { $query = "SELECT * FROM results WHERE lastName LIKE '%$lastName%' AND firstName LIKE '%$firstName'"; } $result = mysql_query($query, $conn); print "n"; print "n"; while ($field = mysql_fetch_field($result)){ print " n"; } print "nn"; while ($row = mysql_fetch_assoc($result)){ print "n"; foreach ($row as $col=>$val){ print " n"; } print "nn"; } print "
$field->name
$val
n"; ?> [/php]

#5

If I leave it blank, the field for firstName or lastName, it still does the error. I can’t figure it out. I mean the script you gave me earlier using the !empty and ? worked for my last script with the form emailer, but why isn’t it working here? It should, there is no reason it shouldn’t.

Is there any way to just turn off pointless notice errors like this one? The script works, the error will be and is just annoying if it stays there any longer. :P


#6

My point with the last post is that you have your IF statement in the wrong order. You set the condition up wrong. You need to either change your condition statement (like I did in my example) or switch the executed statements around.

In response to shutting off annoying errors. The answer is YES. Indeed it can be done. You can do it with the PHP.INI (on error reporting), or you can do it for a particular statement by precceeding it with an “AT” (@) sign. Be warned though. Doing either makes debugging your code that much more difficult. When you execute it you don’t get a message telling you what’s wrong. Your code just doesn’t work.

The more acceptable way is to TRAP errors instead of shutting them off.

Check out http://us4.php.net/errorfunc for more on error reporting.


#7

i think i may have found the source of the undefined index problem

http://www.vdo-specialties.com/results. ... firstName=

That is the website after searching for just a last name…it is still trying to call up a firstName which shouldnt happen.

I am using your new IF statement by the way.