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
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.

Link to comment
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.

 

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.