yogibear Posted August 17, 2008 Share Posted August 17, 2008 Hi I have a search on one of my pages that should search the database using any of the information that has been entered in the form. It almost works but the date needs to be the same as the date in the database for it to find a record. It’s like the “LIKE” doesn’t affect the date, how can I fix this? This is the code I have: $result = mysql_query("SELECT * FROM Record WHERE Date_of_flight LIKE '%$new_date%' AND Pilot_name LIKE '%$Pilot_name%' AND Take_off_airfield LIKE '%$Take_off_airfield%' AND Arrival_field LIKE '%$Arrival_field%' AND Payment_method LIKE '%$Payment_method%'"); Best wishes Yogi Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/ Share on other sites More sharing options...
Mchl Posted August 17, 2008 Share Posted August 17, 2008 $result = mysql_query("SELECT * FROM Record WHERE Date_of_flight= %$new_date% AND Pilot_name LIKE '%$Pilot_name%' AND Take_off_airfield LIKE '%$Take_off_airfield%' AND Arrival_field LIKE '%$Arrival_field%' AND Payment_method LIKE '%$Payment_method%'"); Perhaps you could use '=' even more often? Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/#findComment-618624 Share on other sites More sharing options...
Barand Posted August 17, 2008 Share Posted August 17, 2008 or better ... WHERE Date_of_flight= '$new_date' ... Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/#findComment-618755 Share on other sites More sharing options...
yogibear Posted August 18, 2008 Author Share Posted August 18, 2008 Hi Thanks for your help I have just re-read my post and I don’t think I was clear, sorry I was getting a bit tired of PHP last night. I tried changing the code like you said but no results were found. Here goes my second attempt at my post. I have a form that searches a database, I want it so that it will search using any of the information that has been entered e.g if the pilot name has been entered it will return results with that name even if the date, arrival field or any other field is incorrect. I think I am correct in thinking that is what “LIKE” is used for. It currently works fine on all fields except the date, if I enter the pilot name and the date is the 00/00/0000 it will only return flights for that pilot on that date and not all flights for that pilot which is how I would like it. If you got what I was trying to say on my first post and I do need a = then I apologise, if this is the case how do I stop it returning no results for searches. Best wishes yogi Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/#findComment-619086 Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 Use OR instead of AND Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/#findComment-619089 Share on other sites More sharing options...
yogibear Posted August 18, 2008 Author Share Posted August 18, 2008 Hi Thanks for your help Didn’t fix it but you got me thinking and I found a solution. The problem was the date was always being submitted with a couple of -- even when nothing had been entered for the date, so it was always searching the database using the “--”. So a simple if statement that checked $new_date for “--” solved the problem. This was something that no one who read the post could have known, so thanks for your help. Best wishes Yogi Link to comment https://forums.phpfreaks.com/topic/120085-solved-search-using-a-date/#findComment-619250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.