/    Sign up×
Community /Pin to ProfileBookmark

search returns all records when one hits the search button without filling anything

i have the following search form and script
it works ok but if I hit the search without filling anything on the search box it prints all records whereas i would like it to return No record found. what am I doing wrong?

search form
————————————————————————–

<?
session_start();
if(!session_is_registered(myusername)){
header(“location:main_login.php”);
}
?>

<html>
<body>
Login Successful
</body>
</html><html>

<head>
<meta name=”GENERATOR” content=”Microsoft FrontPage 5.0″>
<meta name=”ProgId” content=”FrontPage.Editor.Document”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<title>Search database</title>
<meta name=”Microsoft Theme” content=”indust 000, default”>
<meta name=”Microsoft Border” content=”tlb, default”>
</head>

<body>

<p>&nbsp;<form method=”post” action=”results.php” target=”_blank”>
<div align=”center”>
<table border=”1″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td bordercolor=”#000000″>
<p align=”center”>
<select name=”metode” size=”1″>
<option value=”consignee”>Consignee</option>
<option value=”datefcrissued”>Date FCR Issued</option>
<option value=”fcrnumber”>FCR Number</option>
</select>
<input type=”text” name=”search” size=”25″ value=””> &nbsp;<br>
Search : <input type=”submit” value=”Go!!” name=”Go”></p>
</td>
</tr>
</table>
</div>
</form>

</body>

</html>

————————————————————————–script

<html>
<head>
<meta name=”GENERATOR” content=”Microsoft FrontPage 5.0″>
<meta name=”ProgId” content=”FrontPage.Editor.Document”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<title>id</title>
<meta name=”Microsoft Theme” content=”indust 000, default”>
<meta name=”Microsoft Border” content=”tlb, default”>
</head>
<body>
<center>
<table border=”1″ cellpadding=”5″ cellspacing=”0″>
<tr>
<td width=”50″><b>DRN</b></td>
<td width=”100″><b>Loading Date</b></td>
<td width=”100″><b>L/O Number</b></td>
<td width=”100″><b>Ginnery</b></td>
<td width=”100″><b>Origin country</b></td>
<td width=”100″><b>Supplier</b></td>
<td width=”150″><b>Delivery Number</b></td>
<td width=”100″><b>Lot Number</b></td>
<td width=”100″><b>No of Bales</b></td>
<td width=”100″><b>Type</b></td>
<td width=”100″><b> Truck Reg. No.</b></td>
<td width=”100″><b>Container No</b></td>
<td width=”100″><b>FCR Number</b></td>
<td width=”100″><b>FCR Date</b></td>
</tr>
<tr>
<td>
<?php
$hostname = “88888888888889”; // The Thinkhost DB server.
$username = “888888888888”; // The username you created for this database.
$password = “i888888888”; // The password you created for the username.
$usertable = “cotton”; // The name of the table you made.
$dbName = “livercot”; // This is the name of the database you made.

//Get the connection handle
$conn = mysql_connect($hostname, $username, $password) OR DIE(“DB connection failed”);
//Add the connection param
mysql_select_db( “$dbName”,$conn) or die( “Unable to find database”);

//error message (no record message)
$ERR = “Sorry we can’t find that record.”;
//query details table begins
$XX = “No Record Found”;

$query = mysql_query(“SELECT * FROM cotton WHERE $metode LIKE ‘%$search%’ LIMIT 0, 30”);

// if > 0 rows are returned
if(mysql_num_rows($query))
{
while ($row = mysql_fetch_array($query))
{
//These variables don’t need to be set… See below
$variable1=$row[“documentsreceivednote”];
$variable2=$row[“loadingdate”];
$variable3=$row[“loadingorderno”];
$variable4=$row[“ginnery”];
$variable5=$row[“origincountryofgoods”];
$variable6=$row[“supplier”];
$variable8=$row[“deliverynumber”];
$variable9=$row[“lotnumber”];
$variable10=$row[“noofbales”];
$variable11=$row[“type”];
$variable12=$row[“truckregnumber”];
$variable13=$row[“containernumber”];
$variable14=$row[“fcrnumber”];
$variable15=$row[“fcrdate”];
//table layout for results

print (“<tr>”);
print (“<td>” . $row[“documentsreceivednote”] . “</td>”);
print (“<td>” . $row[“loadingdate”] . “</td>”); //Add the variables with concatenation
print (“<td>” . $row[“loadingorderno”] . “</td>”);
print (“<td>” . $row[“ginnery”] . “</td>”);
print (“<td>” . $row[“origincountryofgoods”] . “</td>”);
print (“<td>” . $row[“supplier”] . “</td>”);
print (“<td>” . $row[“deliverynumber”] . “</td>”);
print (“<td>” . $row[“lotnumber”] . “</td>”);
print (“<td>” . $row[“noofbales”] . “</td>”);
print (“<td>” . $row[“type”] . “</td>”);
print (“<td>” . $row[“truckregnumber”] . “</td>”);
print (“<td>” . $row[“containernumber”] . “</td>”);
print (“<td>” . $row[“fcrnumber”] . “</td>”);
print (“<td>” . $row[“fcrdate”] . “</td>”);
print (“</tr>”);
}
}
else
{
print (“$ERR”);
}
//end
?>

</body>

</html>

?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarAug 08.2006 — [code=php]$query = mysql_query("SELECT * FROM cotton WHERE ($metode LIKE '%$search%' AND '$search' <> '') LIMIT 0, 30");[/code]
Copy linkTweet thisAlerts:
@balloonbuffoonAug 08.2006 — Unless you have an option to view all records, I find it useful on websites where I can type nothing in the search bar and get all the records, for browsing. Something to think about.

--Steve
×

Success!

Help @jahsymon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...