/    Sign up×
Community /Pin to ProfileBookmark

search function brings all records when blannk input submitted

Is it typical, or is it something wrong with the code?

If you get to the search page from another page via submitted form, and if you had a blank form, you get “no keywords” message.

Now if you are on the search page already, and you hit “search” button with a blank input field, you no longer get “no keywords” message but rather all of the records are being displayed. Given that I have over 4000 records, it is definitely not something I am looking to do.

Here is the code, maybe it is something that should be adjusted there?…

[code=php]
<?php
if (isset($search))
{
mysql_connect(APP_DB_HOST, APP_DB_USER, APP_DB_PASSWORD) or die (“Problem connecting to Database”);
$srch = “%”.$search.”%”;
$query = “SELECT * FROM table WHERE field_one LIKE ‘%$search%’ OR field_two LIKE ‘%$search%'”;
$result = mysql_db_query(“db_mydomain_com”, $query);

if (mysql_num_rows($result)>0)
{
echo “<h4>SEARCH RESULTS</h4>”;
echo “<table>”;
echo “n”;
while ($rtr = mysql_fetch_array($result)) {
// START WHILE LOOP
$one = $rtr[“field_one”];
$two = $rtr[“field_two”];
echo “<tr>n”;
echo “<td class=”small”>$one</td>n”;
echo “<td class=”small”>$two</td>n”;
echo “</tr>n”;
}
// STOP WHILE LOOP
echo “</table>n”;
echo “n”;
}

else {
echo “there were no results found”;
}
}
else {
echo “there were no keywords defined”;
}
?>
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@DariaauthorFeb 28.2006 — So far the only work-around I could come up with was not to include the search form again on the search results page, but rather have a link to "search again" that will take you back to the original page with the form and then have to go through submitting again. Maybe it's not a big deal, but if there is a way to save a user an extra step, I'd like to find it.

All help is appreciated, as always!
Copy linkTweet thisAlerts:
@NogDogFeb 28.2006 — if $search is empty, then your where clause will be [b]WHERE field_one LIKE '%%'[/b] which does, in fact, equate to "match anything and everything." You either need to put in a test to make sure it's not empty, or you could even change the initial line to:
[code=php]
<?php
if(!empty($search))
{
.....
[/code]

That way you'll only enter the search block if $search is set and it's got some content.
Copy linkTweet thisAlerts:
@DariaauthorFeb 28.2006 — I'll try that, thanks!!!
Copy linkTweet thisAlerts:
@DariaauthorFeb 28.2006 — Perfect, thank you so much.
Copy linkTweet thisAlerts:
@NogDogFeb 28.2006 — Perfect, thank you so much.[/QUOTE]
You're welcome. ?
Copy linkTweet thisAlerts:
@DariaauthorAug 16.2006 — Hello! It's been a while ?

If have 2 input fields - for example $search & $find to search to narrow search results

how can I modify this:

[b]if(!empty($search)) [/b]

to make it work. I tried to separate by comma, but, obviously, with no luck.

Thanks!
Copy linkTweet thisAlerts:
@NogDogAug 16.2006 — [code=php]
// if both must have values:
if(!empty($search) and !empty($find)) {
// if at least one must have a value:
if(!empty($search) or !emtpy($find)) {
[/code]

Note: if 0 (zero) would be a valid search value, then the correct way to do this would be:
[code=php]
if((isset($search) and $search !== "") or (isset($find) and $find !== "")) {
[/code]
Copy linkTweet thisAlerts:
@DariaauthorAug 16.2006 — Thanks! I used the third option, works like a charm. It is clear to me now, thanks!

As always - you rock! ?
×

Success!

Help @Daria 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.18,
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,
)...