Jump to content

searching in database.....?


mvleus

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
Link to comment
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.
Link to comment
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.
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.