Forum Moderators: coopster

Message Too Old, No Replies

search function in php

         

UnikRasu

6:55 pm on Dec 5, 2002 (gmt 0)

10+ Year Member



i need help whit a search funktion in my php file i want to hav a form whit two <input type=text> and it shall search my mysql database in two specific columns

anybody how knows how to do this?

/ rasmus

UnikRasu

7:34 pm on Dec 5, 2002 (gmt 0)

10+ Year Member



this is my files

sokvideo.php

<html>
<head>
<title>sök video</title>
</head>
<body>

<h1> sök en film!</h1><br>
<form action = "sok_result.php" method = post>
Namn : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text name=soknamn><br>

<br><br>

<input type=submit name=submit value="Sign">
<input type=reset name=reset value="Reset">

</form></body>
</html>

sok_result.php

<html>
<head>
<title>sök databas resultat</title>
</head>
<body>

<?php
$db = "video";
$table = "video1";

// connecting to MySQL

$conn = mysql_connect("localhost", "root") or
die("Count not connect to database");
echo "connected to database.<br>";

// selecting database

mysql_select_db($db) or
die ("Could not select database");
echo "selcted database $db.<br>";

// displaying the contents of video1

echo "<h2>Videoboken</h2>";
$query = "select * from $table where (soknamn) like ('soknamn')";
$result = mysql_query($query) or
die(mysql_error());

while ($row = mysql_fetch_array($result))
{
echo "<b>Namn:</b>";
echo $row["namn"];
echo "<br>";
echo "<b>Nummer:</b>";
echo $row["nummer"];
echo "<br>";
echo "<b>Tid:</b>";
echo $row["tid"];
echo "<br><br>";
}
?>

<h2><a href = "videobok.htm">Lägg till film</a></h2>
</body>
</html>

and i get this error message
Unknown column 'soknamn' in 'where clause'
i have tried many things and it vill not work

toadhall

7:53 pm on Dec 5, 2002 (gmt 0)

10+ Year Member



If soknamn is the name of the column and soknamn is an entry in that column then this statement will retrieve it:

select * from $table where soknamn like 'soknamn'

...but so will...

select * from $table where soknamn = 'soknamn'

If the soknamn you refer to in the mysql statement is the result of the form, that is, <input type=text name=soknamn> then you'll need to add a $, as in:

select * from $table where soknamn like '$soknamn'

Birdman

7:55 pm on Dec 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>$query = "select * from $table where (soknamn) like ('soknamn')";

Try this:

$query = "SELECT * FROM $table WHERE soknamn LIKE '%soknamn%'";

<also>If you have phpMyAdmin, experiment with your queries from there to get the proper results, then add it to your search function.

Birdman

7:59 pm on Dec 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually my previous post is incorrect. That query will give you a match anytime it sees that string, even if it is within a larger string or word. Just remove the %% for exact match.

UnikRasu

8:05 pm on Dec 5, 2002 (gmt 0)

10+ Year Member



no

sok namn is the name of text area that you are writng what you serch for
and the columns name is namn

i want to get the <input type=text> name to a variable so i can use it to get a value that this script will search for in the column namn

Birdman

8:12 pm on Dec 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$query = "SELECT * FROM $table WHERE soknamn LIKE '$soknamn'";

andreasfriedrich

8:25 pm on Dec 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Given a form like this

<form action = "sok_result.php" method = post>
Namn : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text name=soknamn><br>
<input type=text name=soknamn2><br>

you can access the values the user submitted like so

$_POST['soknamn'];
$_POST['soknamn2'];

Variables from outside PHP [php.net]

Andreas

UnikRasu

9:58 pm on Dec 5, 2002 (gmt 0)

10+ Year Member



it dosent work