Jump to content

[SOLVED] find all instances


wkilc

Recommended Posts

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

Link to comment
Share on other sites

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%'";

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.