Hi, I have a bit of a complex php script. Details are: Background Have a simple four field form. Fields are: Name County Date Start Date To I need the visitor to be able type in all of the above, none of the above or any combination and results from a MySQL database are shown which are narrowed down from the visitors choices above. For example if they leave all four fields empty then all results will be displayed ...or... if they choose just the county, all results will be displayed that match county ... or ... if they choose county and date to, all results will be displayed that match county and entries up until date to field etc... Problem I can't seem to get this to work I have the following: if (($_POST['start'] == "")) { $start = '1900-01-01' ; } else { $start = $_POST['start'] ; } if (($_POST['to'] == "")) { $to = '2050-01-01' ; } else { $to = $_POST['to'] ; } if (($_POST['name'] == "") && ($_POST['County'] == "")) { $result = mysql_query("select * from table WHERE date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else if (($_POST[name] == "")) { $result = mysql_query("select * from table WHERE county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else if (($_POST[County] == "")) { $result = mysql_query("select * from table WHERE name='$name' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else { $result = mysql_query("select * from table WHERE name='$name' and county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } $result1 = mysql_num_rows($result); Code (markup): Not sure if I have over analysed for this but the results do not match what is being chosen. Any help greatly appreciated
You can test empty POST data by doing this if (!$_POST['data']) // or if (!isSet($_POST['data'])) PHP: And you don't need to wrap it around parenthesis, it just looks more confusing than it already is. Don't assign new variables if you only going to use it once. How to put arrays in a string? The way you specify your arrays, you need to use the '', like $_POST['data'] NOT $_POST[data], I saw you used $_POST[County] $result = mysql_query("select * from table WHERE county='{$_POST['County']}' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); PHP: And this is considered a simple script, it is not complex.
I usually attack something like that by adding temp debugging code in it. My methed is to do something like this echo '$this var name = ', $var_name;?><BR><? You will be able to tell the value, see if it is correct. Work your way down through the script with the error message. When I use it in an "if" statement I'll usually put on before the if and one inside the if. Then I can tell if my variable is being picked up by the if. Hope that helps
Thanks for the reply guys but despite the changes this still appears not to be working. The new code is if (!$_POST['start']) { $start = '1900-01-01' ; } else { $start = $_POST['start'] ; } if (!$_POST['to']) { $to = '2050-01-01' ; } else { $to = $_POST['to'] ; } if ((!$_POST['name']) && (!$_POST['County'])) { $result = mysql_query("select * from announce WHERE date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else if (!$_POST['name']) { $result = mysql_query("select * from announce WHERE county='{$_POST['County']}' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else if (!$_POST['County']) { $result = mysql_query("select * from announce WHERE name='{$_POST['name']}'' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } else { $result = mysql_query("select * from announce WHERE name='$name' and county='$County' and date BETWEEN '$start' and '$to' ") or die(mysql_error()); } $result1 = mysql_num_rows($result); Code (markup): Surely if I submit the form with just the County it should display just entries in the database under that County - it's displaying all records ?
maybe you should do a more strict test } else if (!$_POST['name'] && $_POST['County']) { } else if ($_POST['name'] && !$_POST['County']) { PHP:
The first method should be avoided, because it'll throw an E_NOTICE if it's not set. And the second might throw an E_STRICT error in future versions of PHP, because they don't want to allow the incorrect case of function names. (Probably to speed PHP up). It should be isset() - All lowercase.
The first method should not give errors, I've been using it, and no errors found in log. 2nd method, I just change the case so it's easier to read, in practice, I don't emphasize my functions.