Jump to content


Photo

PHP Simple Search


  • Please log in to reply
11 replies to this topic

#1 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 05:29 PM

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:

<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>


Heres the search.php


<?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";
} 

?>


#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 20 July 2006 - 05:36 PM

Ok, perhaps there is something wrong wiith your query; add an or die statement:
$result=mysql_db_query($query) or die(mysql_error());

#3 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 05:40 PM

still get the same thing :(

#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 20 July 2006 - 05:42 PM

Ok, well im not sure if if($result) that will work...try:
if($result != false)

#5 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 20 July 2006 - 05:43 PM

and better to write this line

$query="SELECT * FROM Assembly WHERE PARTNUMBER='$search'";

like this

$query="SELECT * FROM Assembly WHERE PARTNUMBER='" . $_POST['search'] . "'";

i mean use $_POST :) mybe you register_globals is off in php.ini
Sorry for my bad English, it's not my mother language and I am still learning it :)

#6 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 05:43 PM

negative still just getting..

Search string is empty.
Go back and type a string to search

#7 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 20 July 2006 - 05:49 PM

Ok , try "LIKE" as alternative for "="

$query="SELECT * FROM Assembly WHERE PARTNUMBER='$search'";

to

$query="SELECT * FROM Assembly WHERE PARTNUMBER LIKE '%" . $_POST['search'] . "%'";

Sorry for my bad English, it's not my mother language and I am still learning it :)

#8 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 05:51 PM

damn no good i wish it worked thanks for the input.

#9 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 20 July 2006 - 06:00 PM

Try changing this:
if($search)
to
if(!empty($_POST['search']))

#10 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 06:01 PM

now i just get a blank page.

#11 MaaSTaaR

MaaSTaaR
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationKuwait

Posted 20 July 2006 - 06:05 PM

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" ?
Sorry for my bad English, it's not my mother language and I am still learning it :)

#12 dnienhaus

dnienhaus
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 20 July 2006 - 06:08 PM

you are a php freak! Yeah you fixed it it was the mysql_db_query messing it up! Thanks a million!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users