First things first we need to alter your code to enable some debugging of the code:
I have added curley braces as I find them easier to read. (more on those herE: Curly braces in string in PHP). I have also added backticks to the SQL which are used to mark field names.
$sql="SELECT * FROM $table WHERE
`{$field}` LIKE'%{$fieldvalue}%' AND
`{$filter1}` LIKE'%{$filter1value}%' AND
`{$filter2}` LIKE'%{$filter2value}%' AND
`{$filter3}` LIKE'%{$filter3value}%' AND
`{$filter4}` LIKE'%{$filter4value}% ;";
echo $sql; // this allows you to see the SQL you are making
$result=mysql_query($sql)or die('ERROR 238 - ' . mysql_error());
Now obviously when you want other people to use your code then you will need to comment out the echo
line. However if you are getting SQL
errors it helps to see the SQL. As I cannot see your SQL I am going to make a series of educated guesses and provide answers for each one.
Guess 1: Not setting all strings.
My gut reaction is that $fieldvalue
is not set or is an empty string. If you don't see any content for one or more of the place holders and you see a pair of backticks or '%%'
then the problem is that somewhere else in your code the string is not getting a value.
So for this solution check that you are building the string from your user input.
Guess 2: Form not supplying all the values
As you are dealing with user supplied values I hope that you have something nice and solid like this:
if(isset($_POST['fieldvalue']){
$fieldvalue = mysql_real_escape_string(trim($_POST['fieldvalue']));
if($fieldvalue == ''){
$fieldvalue='SomeDefaulValue';
}
}
For each and every value.
Personally that seems like too much work and I would use an array but that's me.
Assuming that you are not proving defaults (quite a common mistake) and the form is, for some reason, not giving you all the values you need this would also result in the same output as not setting the string.
Guess3: An easily overlooked string name difference
It is always worth checking that you did not set the value to $feildvalue
and then call for $fieldvalue
(notice the slight spelling difference.
It happens to us all (and another good argument is made for an array and some loops (less to type and get wrong).