Jump to content

Archived

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

dnienhaus

PHP Simple Search

Recommended Posts

I am trying to make a simple search for an employee database that just needs to search the partnumber and then return a few fields but i can't get it to work. Any help would be greatly appreciated. Thanks..

It jumps straight to this "Search string is empty. Go back and type a string to search."

Heres the form:

[code]<html>
<head><title>Searching...</title>
</head>
<body bgcolor=#ffffff>
<h1>Searching the Database</h1>
<form method="post" action="search.php">
<table width=90% align=center>
<tr><td>search for:</td><td><input type=text name='search' size=60 maxlength=255></td></tr>
<td></td><td><input type=submit></td></tr>
</table>
</form>
</body>
</html>[/code]


Heres the search.php


[code]<?php
$username="edit";
$password="edit";
$database="engineering";


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if($search)
{
$query="SELECT * FROM Assembly WHERE PARTNUMBER='$search'";
$result=mysql_db_query($query);

if($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF><b>Creation Date</b></td>
<td align=center bgcolor=#00FFFF><b>Creator</b></td>
<td align=center bgcolor=#00FFFF><b>Nomenclature</b></td>
<td align=center bgcolor=#00FFFF><b>OBID</b></td>
<td align=center bgcolor=#00FFFF><b>Part Number</b></td>
</tr>";

while ($r = mysql_fetch_array($result)) { // Begin while
$CREATIONDATE = $r["CREATIONDATE"];
$CREATOR = $r["CREATOR"];
$NOMENCLATURE = $r["NOMENCLATURE"];
$OBID = $r["OBID"];
$PARTNUMBER = $r["PARTNUMBER"];
echo "<tr>
<td>$CREATIONDATE</td>
<td>$CREATOR</td>
<td>$NOMENCLATURE</td>
<td>$OBID</td>
<td><b>$PARTNUMBER</b></td></tr><br><br><hr>";
} // end while
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}

?>[/code]

Share this post


Link to post
Share on other sites
Ok, perhaps there is something wrong wiith your query; add an or die statement:
$result=mysql_db_query($query) or die(mysql_error());

Share this post


Link to post
Share on other sites
and better to write this line

[code=php:0]$query="SELECT * FROM Assembly WHERE PARTNUMBER='$search'";[/code]

like this

[code=php:0]$query="SELECT * FROM Assembly WHERE PARTNUMBER='" . $_POST['search'] . "'";[/code]

i mean use $_POST :) mybe you register_globals is off in php.ini

Share this post


Link to post
Share on other sites
Ok , try "LIKE" as alternative for "="

[code=php:0]$query="SELECT * FROM Assembly WHERE PARTNUMBER='$search'";[/code]

to

[code=php:0]$query="SELECT * FROM Assembly WHERE PARTNUMBER LIKE '%" . $_POST['search'] . "%'";[/code]

Share this post


Link to post
Share on other sites
hmmm , ok better to use mysql_query not mysql_db_query , try .

and try to add echo '<h1>test</h1>'; above the query , and see will the page print "test" ?

Share this post


Link to post
Share on other sites
you are a php freak! Yeah you fixed it it was the mysql_db_query messing it up! Thanks a million!

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.