Jump to content

[SOLVED] search results show all results from table if feild is left empty


hidden_pearl

Recommended Posts

Hi,

 

i am stuck with this search code ....

 



$query = " SELECT * FROM table WHERE  
     fisrt_name LIKE '$first_name%'
OR last_name LIKE '$last_name%'
OR family_name LIKE '$family_name%'

";
   

 

the problem is: i want to allow users to leave empty feilds in search form for first_name, last_name and family_name. but when a user leave any feild empty then search shows all data from table. all what i want is, that if user leaves any feild empty then it must not include for search parameters.

 

can anyone plz guid me where i am wrong or what should i do.

(plz let me know if i have not explained properly)

 

Regards,

 

 

Link to comment
Share on other sites

Something like

$query = 'SELECT * FROM table';

$where = array();

if (!empty($first_name)) {
$where[] = "first_name LIKE '%" . mysql_real_escape_string($first_name) . "%'";
}

// same thing for the other fields

if (count($where)) {
$query .= 'WHERE ' . join(' OR ', $where);
}

Link to comment
Share on other sites

i have triend this code

<?php


$query = 'SELECT * FROM upload';

$where = array();

if (!empty($fname)) {
$where[] = "fname LIKE '%" . mysql_real_escape_string($fname) . "%'";
}
if (!empty($lname)) {
$where[] = "lname LIKE '%" . mysql_real_escape_string($lname) . "%'";
}
if (!empty($fmname)) {
$where[] = "fmname LIKE '%" . mysql_real_escape_string($fmname) . "%'";
}


// same thing for the other fields

if (count($where)) {
$query .= 'WHERE ' . join(' OR ', $where);
}

php?>

 

and it gave me same result ...i.e. if user left feild empty then it shows all results

 

moreover when i enter in any feild it gives query failed error...

 

by the way rest of the main code is

 

<?php
$result = mysql_query($query) or die('Error, query failed');
$numrows =mysql_num_rows($result);
echo"<br>Total Results = $numrows<br>";


while ($db_field= mysql_fetch_assoc($result)) {
echo"ID: <b>$db_field[id] </b><br>"; 
echo"First: <b><font color=red>$db_field[fname]</b></font><br>";
echo" Last:<b> $db_field[lname]</b><br>";
echo"Family: <b>$db_field[fmname]</b><br><br><br>"; 
}
?>

 

plz help

 

Link to comment
Share on other sites

kindly check this link

 

LINK

 

when u try to search with any empty value then it shows all of data from "upload" table.

 

i want to set search parameters in a way that

---if user leaves first name empty then search query will select from posible results from last name and family name.

---if user leaves last name empty then search query will select from posible results from fisrt name and family name.

---if user leaves family name empty then search query will select from posible results from first name and last name.

 

right now if i left all feilds empty then instead of showing 0 results its showing all data.

 

 

Link to comment
Share on other sites

I suppose you could change

if (count($where)) {
   $query .= 'WHERE ' . join(' OR ', $where);
}

to

if (count($where)) {
   $query .= 'WHERE ' . join(' OR ', $where);
}
else {
   $query .= 'WHERE 1=0';
}

 

That would result in no results if all fields are left empty. If you regard all fields empty as an invalid search operation, why not give the user an error instead though?

Link to comment
Share on other sites

If you regard all fields empty as an invalid search operation, why not give the user an error instead though?

 

yes giving error mesage to users is the best option. but see if i fil in last name and first name is empty then still it gives error. it doesnot run query ...

Link to comment
Share on other sites

 

<?php $query = " SELECT * FROM upload WHERE  
  fname LIKE '$fname%'
OR lname LIKE '$lname%'
OR fmname LIKE '$fmname%'
OR yb = '$yb'
OR mb = '$mb'
OR db = '$db'
OR yd = '$yd'
OR md = '$md'
OR dd = '$dd'

";?>

i have set this error by myself

<?php $result = mysql_query($query) or die('Error, query failed');
?>

 

so it says "'Error, query failed"  :wtf:

 

 

 

 

Link to comment
Share on other sites

thanx for sharing a wonderful info..

 

i have removed or die ("")

 

but still if all feilds r empty then it shows all data table in result and if one out of 3 feilds r filled then it give this message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xcv1001/public_html/saima/pic_upload/test3.php on line 80 and 84

 

and line 80 is

<?php $numrows =mysql_num_rows($result);

and 84 is

<?php while ($db_field= mysql_fetch_assoc($result)) {... 

 

 

 

 

Link to comment
Share on other sites

 

thanx for sharing this info ... i tried this code:

<?php SELECT * FROM upload WHERE  1=0
  fname LIKE '$fname%'
OR lname LIKE '$lname%'
OR fmname LIKE '$fmname%' ?>

 

and it worked by adding "WHERE  1=0"....

 

but anyway ,today from ur replies i have learnt 3 new things .... am greatful to u 4 that  :-*

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.