Jump to content

[SOLVED] PHP/MySQL Query Question


craig1978

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/48104-solved-phpmysql-query-question/
Share on other sites

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.

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.