Jump to content

Recommended Posts

I have this form

<form id="submit-search-query" name="submit-search-query" method="get" action="">
   <label>
   <center><input name="search" type="text" class="show-search-box-text-box" size="20"/>
   </center>
   </label>
   <label>
   <center><input name="submit-search" type="submit" class="show-search-box-submit-button" value="Search" />
   </center>
   </label>
   </form>

 

and i want it to search my database and echo out the search results that have to word what was entered in the form.

 

Can anyone help thanks

 

Shaun

Link to comment
https://forums.phpfreaks.com/topic/120083-making-a-form-thats-searches-database/
Share on other sites

You would have to set the action to another page maybe search.php.

 

Your search.php would look something like this

<?php
$host = "mysqlhost";
$user = "user";
$pass = "pass";
$db = "db";
$table = "table"

$search = $_POST['search'];

$connect = mysql_connect($host, $user, $pass);

mysql_select_db($db);

$query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here!

if(!$search)
{
echo "Please enter a search";
}
else
{
$result = mysql_query($query);
}

while($row = mysql_fetch_array($result))
{
extract($row)

echo $row['data']; //you would change data to the data that you want displayed.
}

?>

 

This is kind of a rough draft but you should get the point and can modify for your needs.

You would have to set the action to another page maybe search.php.

 

Your search.php would look something like this

<?php
$host = "mysqlhost";
$user = "user";
$pass = "pass";
$db = "db";
$table = "table"

$search = $_POST['search'];

$connect = mysql_connect($host, $user, $pass);

mysql_select_db($db);

$query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here!

if(!$search)
{
echo "Please enter a search";
}
else
{
$result = mysql_query($query);
}

while($row = mysql_fetch_array($result))
{
extract($row)

echo $row['data']; //you would change data to the data that you want displayed.
}

?>

 

This is kind of a rough draft but you should get the point and can modify for your needs.

 

Got a problem...

 

Its saying Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\bizlizard\link-search.co.nr new design\index.php on line 89

 

I don't know why i am getting this.

try this

 

<?php
$host = "mysqlhost";
$user = "user";
$pass = "pass";
$db = "db";
$table = "table";

$search = $_POST['search'];

$connect = mysql_connect($host, $user, $pass);

mysql_select_db($db);

$query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here!

if(!$search)
{
echo "Please enter a search";
}
else
{
$result = mysql_query($query);
}

while($row = mysql_fetch_array($result))
{
extract($row);

echo $row['data']; //you would change data to the data that you want displayed.
}

?>

try this

 

<?php
$host = "mysqlhost";
$user = "user";
$pass = "pass";
$db = "db";
$table = "table";

$search = $_POST['search'];

$connect = mysql_connect($host, $user, $pass);

mysql_select_db($db);

$query = "SELECT * from $table WHERE search"; //You would want to put in your specific search here!

if(!$search)
{
echo "Please enter a search";
}
else
{
$result = mysql_query($query);
}

while($row = mysql_fetch_array($result))
{
extract($row);

echo $row['data']; //you would change data to the data that you want displayed.
}

?>

 

hmm...

 

now i am getting this...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\bizlizard\link-search.co.nr new design\index.php on line 85

Don't know why tho.

 

 

Could i some how make it so it goes to another page like so...

 

someone submits the form and then it sends the data and the user to search.php and the results are there?

 

Ok,

 

you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION.

 

first spot..... $query = "SELECT * from $table WHERE search"; 

 

You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'"

 

 

Notice the 3 variables there.... $table is set earlier and should be the name of the table your searching, $fieldName is one I added, replace that with the name of the field your looking in. If you want to look in a field called usernames, then replace it with that. The LIKE %$search% part is a wildcard search pattern.

 

if the letter A was typed in the search field it woud find things like Another (a is the first letter), Panda (a is in the middle and at the end), Antarctica (a is in the beginning, middle and end) , should those things be in the database. the % is the wild card,

 

$row['data']  needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in.

 

Hope this helps some.

 

 

Ok,

 

you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION.

 

first spot..... $query = "SELECT * from $table WHERE search"; 

 

You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'"

 

 

Notice the 3 variables there.... $table is set earlier and should be the name of the table your searching, $fieldName is one I added, replace that with the name of the field your looking in. If you want to look in a field called usernames, then replace it with that. The LIKE %$search% part is a wildcard search pattern.

 

if the letter A was typed in the search field it woud find things like Another (a is the first letter), Panda (a is in the middle and at the end), Antarctica (a is in the beginning, middle and end) , should those things be in the database. the % is the wild card,

 

$row['data']  needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in.

 

Hope this helps some.

 

 

This part i don't understand. :(

you can't use the code verbatim. There are a couple spots where you need to change it to make it work for YOU and YOUR SITUATION.

 

first spot..... $query = "SELECT * from $table WHERE search"; 

 

You need to make it say something like "SELECT * FROM $table WHERE $fieldName LIKE '%$search%'"

This part i don't understand. :(

The LIKE %$search% part is a wildcard search pattern.

This oark i do understand i have done this i will show you my name for the tables field names and the database name. :)

$row['data']  needs to be $row['yourFieldName'] meaning replace that with the name of the field your searching in.

 

Hope this helps some./quote]

 

 

The database name is "bizlizard"

The table name is "websites"

And the field name is "url"

 

Could you, if its not too much trouble, put it all together for me so i can see how it all goes. (Together)(If you want)

 

Thank you very much

 

Shaun

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.