craig1978 Posted April 22, 2007 Share Posted April 22, 2007 Hi Guys, Firstly I am seriously a beginner so please take it slow with your replies. I have about 3 days PHP experience and that's it for my life in computer programming(?) Here goes: I want users to be able to search a database using 2 variables (eventually 3 but I'm stumped at 2). These variables will be entered via a html form. I'm not sure if this is the best way but it's working with the 1 variable. When I try to use the AND command in my sql query i get the following error message: Notice: Undefined index: Age in /clientdata/clients/f/i/fitnesssearch.com.au/www/1.php on line 29 here is line 29 of my code: $query="SELECT * FROM PersonalTrainers WHERE Postcode = ($_POST[Postcode]) AND Age = ($_POST[Age])"; I'm assuming the error is telling me that it can't find Age in my database but I have checked numerous times and it's there. As I'm beginning it will probably be my code syntax that is at fault. Any help would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/48104-solved-phpmysql-query-question/ Share on other sites More sharing options...
shaymol Posted April 22, 2007 Share Posted April 22, 2007 hi craig try with this modifed line $query="SELECT * FROM PersonalTrainers WHERE Postcode = ($_POST['Postcode']) AND Age = ($_POST['Age'])"; Thanks ShaymoL Quote Link to comment https://forums.phpfreaks.com/topic/48104-solved-phpmysql-query-question/#findComment-235088 Share on other sites More sharing options...
bubblegum.anarchy Posted April 22, 2007 Share Posted April 22, 2007 The associative array index for the $_POST data is formatted as a string and needs to be quoted... so $_POST[Age] should be $_POST['Age'], the same goes for Postcode... remove the ( ) brackets from the post data too... something like this may be more appropriate: $query = "SELECT * FROM PersonalTrainers WHERE Postcode = '".$_POST['Postcode']."' AND Age = '".$_POST['Age']."'"; Remember that AND means that both conditions have to be true for the record to be returned... use an OR instead if you require either one of the values to match. Quote Link to comment https://forums.phpfreaks.com/topic/48104-solved-phpmysql-query-question/#findComment-235089 Share on other sites More sharing options...
craig1978 Posted April 22, 2007 Author Share Posted April 22, 2007 The associative array index for the $_POST data is formatted as a string and needs to be quoted... so $_POST[Age] should be $_POST['Age'], the same goes for Postcode... remove the ( ) brackets from the post data too... something like this may be more appropriate: $query = "SELECT * FROM PersonalTrainers WHERE Postcode = '".$_POST['Postcode']."' AND Age = '".$_POST['Age']."'"; Remember that AND means that both conditions have to be true for the record to be returned... use an OR instead if you require either one of the values to match. Thanks Heaps, You both are so great to try to help. The 2nd one worked a treat. Is that due to different versions of MySQL or something?? Now onto my third variable in the form, wish me luck!! Craig Quote Link to comment https://forums.phpfreaks.com/topic/48104-solved-phpmysql-query-question/#findComment-235156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.