r00tk1LL Posted July 18, 2007 Share Posted July 18, 2007 I have a form that users can use to search with multiple fields but sometimes they wont search in all the provided fields. What is the best way to have php query the DB with only what has been submitted instead of sending blank html fields?? Thanks Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/ Share on other sites More sharing options...
PC Nerd Posted July 18, 2007 Share Posted July 18, 2007 um, do validation.... like if(empty($field1)) {} then once you know what is empty, you can query etc from there thats the only way Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301330 Share on other sites More sharing options...
pocobueno1388 Posted July 18, 2007 Share Posted July 18, 2007 You can do something like this: <?php $query = "SELECT * FROM table WHERE 1"; if (isset($_POST['optional_field'])) $query .= " AND optional_field = '{$_POST['optional_field']}"; if (isset($_POST['other_field'])) $query .= " AND other_field = '{$_POST['other_field']}"; $result = mysql_query($query)or die(mysql_error()); ?> Check if they submitted that field, and if they did just add to the query. Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301335 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 OK great I'll try that now one more thing...Could I incorporate "LIKE" into that to use wildcards??? Heres the url if you want to see what all this is about http://www.mynorthtexas.com/mypages/final/ Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301417 Share on other sites More sharing options...
pocobueno1388 Posted July 18, 2007 Share Posted July 18, 2007 Yes, you can use LIKE Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301423 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 I guess the question is can LIKE be used more than 2 times in a query statement? Would this be the correct syntax: $query .= " AND other_field LIKE '%{$_POST['other_field']}%' AND other_field LIKE '%{$_POST['other_field']}%' Could you help me clean this up??? Thanks Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301442 Share on other sites More sharing options...
pocobueno1388 Posted July 18, 2007 Share Posted July 18, 2007 Yes, you can use LIKE two times in one query. Is that syntax not working for you? It looks okay to me, even though I'm not super familiar with using LIKE in my queries. Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301526 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 Hey thanks for helping me, I'm trying this without the LIKE to simplify things. This is not working, is there anything wrong.... //some previous code $link = mysql_connect ( $hostname, $username, $password); //user input variables $query="SELECT uid, image, uname FROM mypages WHERE"; if (isset($_POST['name'])) $query .= " name = '{$_POST['name']}"; if (isset($_POST['age'])) $query .= " AND age = '{$_POST['age']}"; if (isset($_POST['location'])) $query .= " AND location = '{$_POST['location']}"; if (isset($_POST['marital'])) $query .= " AND marital = '{$_POST['marital']}"; if (isset($_POST['hobbies'])) $query .= " AND hobbies = '{$_POST['hobbies']}"; if (isset($_POST['interests'])) $query .= " AND interests = '{$_POST['interests']}"; if (isset($_POST['sex'])) $query .= " AND sex = '{$_POST['sex']}"; //rest of code heres the page so you can see what I'm talking about: http://www.mynorthtexas.com/mypages/final Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301538 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 Ok this doesnt work if (isset($_POST['name'])) $query .= " AND name = '{$_POST['name']}'"; But this does if (isset($_POST['name'])) $query .= " name = '{$_POST['name']}'"; ??? Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301564 Share on other sites More sharing options...
pocobueno1388 Posted July 18, 2007 Share Posted July 18, 2007 Oops, in your initial query, I think you need to set at least one condition. $query="SELECT uid, image, uname FROM mypages WHERE"; So you need to add something after the WHERE. Do you have any field that isn't optional? If you do, then you should put it there. Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301619 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 I see where this is going, could I reference a field that would always be true? For example anyone that has information in the database has a user id 'uid' field... I.E. $query="SELECT uid, image, uname FROM mypages WHERE uid = true"; Or just some kind of trick to allow the user to enter information in any field keeping them all optional?? Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301791 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 OK I'm playing with this idea. I found that I can use this: $query="SELECT uid, image, uname FROM mypages WHERE id > 0"; if (isset($_POST['age'])) { $query .= " AND age = '{$_POST['age']}'"; } And it will work fine no matter what variable I use But if I use more than one: $query="SELECT uid, image, uname FROM mypages WHERE id > 0"; if (isset($_POST['age'])) { $query .= " AND age = '{$_POST['age']}'"; } if (isset($_POST['name'])) { $query .= " AND name = '{$_POST['name']}'"; } The search fails every time no matter what Please someone tell me what this is all about, I've been on this for days. Thanks Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301839 Share on other sites More sharing options...
trq Posted July 18, 2007 Share Posted July 18, 2007 Try echo'ing $query prior to execution to see if there are any errors. Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301844 Share on other sites More sharing options...
r00tk1LL Posted July 18, 2007 Author Share Posted July 18, 2007 here is what it said: SELECT uid, image, uname FROM mypages WHERE id > 0 AND age = '18' AND name = '' It looks like it is grabbing that empty if statement?? Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301849 Share on other sites More sharing options...
trq Posted July 19, 2007 Share Posted July 19, 2007 if (isset($_POST['name']) && (!empty($_POST['name'])) Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301855 Share on other sites More sharing options...
r00tk1LL Posted July 19, 2007 Author Share Posted July 19, 2007 Hey what would be better (more efficient): if (isset($_POST['name']) && (!empty($_POST['name'])) or if ( $_POST['age'] != NULL) { $query .= " AND age = '{$_POST['age']}'"; } Because I found the last one worked just now? Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301860 Share on other sites More sharing options...
pocobueno1388 Posted July 19, 2007 Share Posted July 19, 2007 I would use thorpe's suggestion: if (isset($_POST['name']) && (!empty($_POST['name'])) Are you saying that when you use the above suggestion it doesn't work, or are you just saying that they both work but you think the second one is a better way? Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301866 Share on other sites More sharing options...
r00tk1LL Posted July 19, 2007 Author Share Posted July 19, 2007 They both work, I would just like to use the best one? And then this thread will be solved Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301873 Share on other sites More sharing options...
r00tk1LL Posted July 19, 2007 Author Share Posted July 19, 2007 ;D ;D Ok Guys this is what I was looking for, I guess I'll stick with this for now since it is working. $query="SELECT uid, image, uname FROM mypages WHERE id > 0"; if ($_POST['name'] != NULL) $query .= " AND name LIKE '%{$_POST['name']}%' || uname LIKE '%{$_POST['name']}%'"; if ($_POST['age'] != NULL) $query .= " AND age = '{$_POST['age']}'"; if ($_POST['location'] != NULL) $query .= " AND location LIKE '%{$_POST['location']}%'"; if ($_POST['marital'] != NULL) $query .= " AND marital = '{$_POST['marital']}'"; if ($_POST['hobbies'] !=NULL) $query .= " AND hobbies LIKE '%{$_POST['hobbies']}%'"; if ($_POST['interests']!=NULL) $query .= " AND interests LIKE '%{$_POST['interests']}%'"; if ($_POST['sex'] !=NULL) $query .= " AND sex = '{$_POST['sex']}'"; Thank you guys for your help!!! ;D ;D ;D ;D ;D Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301884 Share on other sites More sharing options...
pocobueno1388 Posted July 19, 2007 Share Posted July 19, 2007 Awesome =] If it works for you, then that is just fine. There isn't a better way out of the two, it's just personal preference. Link to comment https://forums.phpfreaks.com/topic/60570-solved-optional-form-fields/#findComment-301910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.