wright67uk Posted January 13, 2011 Share Posted January 13, 2011 Why do i get no results returned from the below form? Ive tested the string; SELECT phone FROM mytablename WHERE sg ='YES' ORDER BY companyName LIMIT 3 and the sql is fine in mysql admin, and returns the expected results. However in practice I seem to be having difficulties. Is my syntax out of place. All i get returned are the two words "Phone numbers: " Im trying with difficulty to learn php! please any pointers? <code> <!-- html only form.html--> <form method="get" action="phponlyform.php"> <input type="text" name="postcode" size="2" maxlength="2"/> <input type="submit" name="Submit"/> </form> <!-- phponlyform.php--> <?php mysql_connect("myusername","mydatabase… mysql_select_db("mydatabase") or die("Unable to select database"); $code = $_GET['postcode']; $result = mysql_query("SELECT phone FROM mytablename WHERE '$code' ='YES' ORDER BY companyName LIMIT 3") or die(mysql_error()); $row = mysql_fetch_array( $result ); echo "Phone Numbers: ".$row['phone']; ?> <!-- eg. column name is SG and he value is YES --> </code> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2011 Share Posted January 13, 2011 Your query is wrong, or your database design is fundamentally flawed. This: WHERE '$code' ='YES' will never evaluate to TRUE, unless the value coming from the 'postcode' form field is 'YES', and then the query would return all records in the database. This is because in that piece of code, you are checking whether the value of the $code variable is equal to the literal string 'YES'. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted January 13, 2011 Author Share Posted January 13, 2011 the actual database would be somthing like this; phone number / company name / SG / EN / CM / SW / NE / SE 0800 089098 / Company A / YES / NO / YES / YES / YES / YES 0800 111111 / Company B / NO / YES / NO / YES / NO / YES 0800 222222 / Company C / YES / NO / NO / NO / NO / NO 0800 333333 / Company D / NO / NO / NO / NO / NO / NO 0800 444444 / Company E / YES / YES / NO / NO / YES / YES I have it like this, as the companies will subscribe to different postcode areas. This is why for example in my html form the user typed in EN, I would want the phone numbers of company b and company e displayed. Im i right in thinking that I would have to change the values of YES and NO to another word such as "subscribes" and "no subscription"? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2011 Share Posted January 13, 2011 No, you need to change '$code' to `$code`. Quotes are for string values, backticks are for database table and field names. EDIT: Something still seems strange about this, but I can't put my finger on it at the moment for some reason. . . Quote Link to comment Share on other sites More sharing options...
wright67uk Posted January 14, 2011 Author Share Posted January 14, 2011 Thankyou for getting back to me. I did as you said, and im perhaps another step forward, as it seems as if mysql query is being processes. I do however now get a response of ; Unknown column 'yes' in 'where clause' Does this mean that for some reason that the value for `$code` is being totally ignored, leaving 'yes' to be the column name opposed to being a value? heres my browser url eg. If i type EN into my html form; http://www.mydomainhere.co.uk/phponlyform.php?postcode=EN&Submit=Submit+Query Many thanks Quote Link to comment Share on other sites More sharing options...
Skylight_lady Posted January 14, 2011 Share Posted January 14, 2011 Why are you using $_GET['postcode'] ? $_POST['postcode'] is the correct option. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2011 Share Posted January 14, 2011 How do you figure it should be $_POST, when the form method is clearly 'get'? Quote Link to comment Share on other sites More sharing options...
Skylight_lady Posted January 14, 2011 Share Posted January 14, 2011 How do you figure it should be $_POST, when the form method is clearly 'get'? Your right. I ignored the method='get'. Try and echo out $code and see what result you get when you submit the form. Also echo $result; to see if the query is fine. If your $code is left empty in the form then you will get an error like "Unknown column 'yes' in 'where clause'", you will also get that error if the column name written in the form is not in the database. It is better to use a validation statement in the code so it can't be left empty or has to equal to one of the column names. Quote Link to comment 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.