Jump to content

Archived

This topic is now archived and is closed to further replies.

mvleus

searching in database.....?

Recommended Posts

hello everybody,

I am totally new in the big world of PHP and SQL.
I am developing a database for a union i am a member of. We wanted to add all of our members to a database!
So i decided to use MySQL 4 and PHP4. I build the SQL server with the help of PHPmyAdmin and the database itself works perfectly.

I build a website with Dreamweaver that contacts the SQL database. Logging in and retrieving data from the database all works perfectly too.

But now i wanna add a textfield to my website where i can enter the name of a member and by pressing a button i will give the order to search the database. I already have something like this:

[code]
<?php
include ('dbconnect.php');
?>
<h2>Unions memberlist:</h2>
<b>first name:</b>
<input type=text size=40 name=name>
<label>
<input type="submit" name="Submit" value="Search" />
</label>
<br />
<br />

<?php
$result = mysql_query("select * from union_members where firstname = 'Marc'") or
die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>personelnumber: </b>";
echo $row["personelnumber"];
echo "<br>\n";
echo "<b>firstname: </b>";
echo $row["fisrtname"];
echo "<br>\n";
echo "<b>lastname: </b>";
echo $row["lastname"];
echo "<br>\n";
echo "<b>address: </b>";
echo $row["address"];
echo "<br>\n";
echo "<b>number: </b>";
echo $row["number"];
echo "<br>\n";
echo "<b>zipcode: </b>";
echo $row["zipcode"];
echo "<br>\n";
echo "<b>city: </b>";
echo $row["city"];
echo "<br>\n";
echo "<b>phonenumber: </b>";
echo $row["phonenumber"];
echo "<br>\n";
echo "<b>cellphonenumber: </b>";
echo $row["cellphonenumber"];
echo "<br>\n";
echo "<b>date of birth: </b>";
echo $row["date_of_birth"];
echo "<br>\n";
echo "<b>Emailaddress: </b>";
echo $row["emailaddress"];
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
}
mysql_free_result($result);
?>
[/code]


If i open this page with my browser, the pages works perfectly. It shows me all the data i asked for.

But i just dont know how to continue in order to enter the search command by entering the text in the textfield and then pressing the button.

Can anyone please help me?

Kind regards,

mvleus

Share this post


Link to post
Share on other sites
You would need the html form to be like this instead:

[code]
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
  <input type='text' name='name' size='40'>
  <input type='submit' name='Submit' value='Search'>
</form>
[/code]

and then for the query part, use this instead:

[code]
$result = mysql_query("select * from union_members where firstname+lastname like  '%".mysql_real_escape_string($_POST['name'])."%'") or
die (mysql_error());
[/code]

As listed, on the first run it should return everybody, but you can tweak that as desired.

Share this post


Link to post
Share on other sites
If you are really uncomfortable with php and mssql, ignore what I am going to say and use the previous example. 

If you can, look into what is called a full-text index to do your search.  It is much more powerful and more importantly, a lot faster.  It can be a little more difficult to implement, however. There are plenty of resources online.

Share this post


Link to post
Share on other sites

×

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.