Jump to content


Photo

searching in database.....?


  • Please log in to reply
2 replies to this topic

#1 mvleus

mvleus
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 13 October 2006 - 04:18 PM

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:

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


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

#2 MCP

MCP
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 15 October 2006 - 05:16 AM

You would need the html form to be like this instead:

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

and then for the query part, use this instead:

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

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

#3 argoSquirrel

argoSquirrel
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 16 October 2006 - 04:47 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users