wkilc Posted July 26, 2008 Share Posted July 26, 2008 If folks use this text form to search my table for a student named "chris": <form name="myform" action="index.php" method="get"> <b>Search name:</b><br> <input type="text" name="student" /> <input type="submit" value="Go!" /> </form> Students are stored in the table as "lastname, firstname" The result of the query will show: christian, bobby christensen, jane but NOT: jones, chris How can I tweak the form so that it will find EVERY instance of "chris", whether it appears at the beginning, middle, or end of the name? Thank you. ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/ Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 well first off I would make a one-time-only script to pull the "names" out, separate the first from the last names, and store them in separate columns. But anyways... $name = "chris"; $sql = "SELECT studentnamecolumnhere FROM tablenamehere WHERE studentnamecolumnhere LIKE '%$name%'"; Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600181 Share on other sites More sharing options...
wkilc Posted July 26, 2008 Author Share Posted July 26, 2008 Thank you. I don't understand something... $name has to be hard-coded as "chris"? I attempted to simplify my form to make the question easier, but I see now that was a mistake. I currently define $name as the value last passed the form, that way it will display the name the user just searched as the current value in the text field. <?php // If the form has been submitted... if(isset($_GET['submit'])) { // Set $name as the value of the name field in your form ($_GET just references the form) $name = $_GET['name']; } ?> <form name="form" method="get"> <input type="text" name="name" value="<? echo $name; ?>" /> <input type="submit" name="submit" value="Search" /> </form> How do I proceed from here? Thanks again. ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600205 Share on other sites More sharing options...
MFHJoe Posted July 26, 2008 Share Posted July 26, 2008 $name doesn't have to be hardcoded as 'Chris'. You can do it the way you wanted to with $_GET. $name = $_GET['name']; $sql = "SELECT studentnamecolumnhere FROM tablenamehere WHERE studentnamecolumnhere LIKE '%$name%'"; Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600207 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 no, $name does not have to be hardcoded as chris. I was giving an example variable so I could show how it's used in the query string. You can do whatever you want to to assign something to $name. p.s.- You should be using method="post" in your form and $_POST['name']. It's (more) secure. Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600208 Share on other sites More sharing options...
wkilc Posted July 26, 2008 Author Share Posted July 26, 2008 Thank you. Not sure where to implement it... this is still not working... no errors, but still behaving as before. <?php // If the form has been submitted... if(isset($_POST['submit'])) { // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['name']; } $name = $_POST['name']; $sql = "SELECT studentnamecolumnhere FROM tablenamehere WHERE studentnamecolumnhere LIKE '%$name%'"; ?> <form name="form" method="post"> <input type="text" name="name" value="<? echo $name; ?>" /> <input type="submit" name="submit" value="Search" /> </form> I'm defining $name twice, that can't be right? ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600218 Share on other sites More sharing options...
mattal999 Posted July 26, 2008 Share Posted July 26, 2008 <?php // If the form has been submitted... if(isset($_POST['submit'])) { // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['name']; $sql = "SELECT studentnamecolumnhere FROM tablenamehere WHERE studentnamecolumnhere LIKE '%$name%'"; // Do your mysql query to check through the table } ?> <form name="form" method="post"> <input type="text" name="name" value="<? echo $name; ?>" /> <input type="submit" name="submit" value="Search" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600221 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 Also, you need to replace studentnamecolumnhere and tablenamehere with your real column and table name... and I assume that you aren't showing your whole code, because I don't see anywhere where you're actually displaying any info (or even executing the query) Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600224 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 no, $name does not have to be hardcoded as chris. I was giving an example variable so I could show how it's used in the query string. You can do whatever you want to to assign something to $name. p.s.- You should be using method="post" in your form and $_POST['name']. It's (more) secure. Well, not necessarily more secure...just takes more than an idiot to tamper with. But whatever. =P Also, I'd personally do what Crayon Violent said originally and create a lastname and firstname column. It's better because it creates much easier searches and such. Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600226 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 well that's why i put "more" in parentheses. Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600231 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 well that's why i put "more" in parentheses. =P Just clarifying. Anyway, if you need help fixing your database to work with a new structure, I'll write a quick "database fixing script" for you. Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600235 Share on other sites More sharing options...
wkilc Posted July 26, 2008 Author Share Posted July 26, 2008 Thanks yet again... I've changed the table values... At the looking REALLY ignorant... I'm still missing something if I use mattall999's code, right? <?php // If the form has been submitted... if(isset($_POST['submit'])) { // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['name']; $sql = "SELECT studentname FROM mytablename WHERE studentname LIKE '%$name%'"; // Do your mysql query to check through the table } ?> // Do your mysql query to check through the table It's still behaving as before. What might the query look like? ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600244 Share on other sites More sharing options...
wkilc Posted July 26, 2008 Author Share Posted July 26, 2008 In other words, I'm still getting this: index.php?name=chris Rather than this: index.php?name=%chris% ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600255 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 You're not actually performing the query... Do you know how to use MySQL from within PHP? Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600256 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 I was under the impression your script was already working to a certain degree; that is, you're getting some of the names you want, but not all...so...where is your mysql_query? Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600257 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 In other words, I'm still getting this: index.php?name=chris Rather than this: index.php?name=%chris% ~Wayne And you shouldn't be getting either one of those. That's the GET method. You've changed to the POST method. So you shouldn't be getting any name='s in your url. The %..% is added to your query string it's not passed from your form from any method. Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600260 Share on other sites More sharing options...
wkilc Posted July 26, 2008 Author Share Posted July 26, 2008 My bad!!! I just made something simple into a HUGE task. Problem solved. Thanks again for help and your patience. ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/116724-solved-find-all-instances/#findComment-600261 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.