2

I am working in php and phonegap. i am sending title, category of song using url to php page. now i want to select songs according to title and category if these both are provided in url. if url providing only title then search must be done according to title only. and if url providing category only then search must be basis on the category only.

**this is code which i am trying**

$titletosearch = $_GET["title"];
$categorytosearch = $_GET["category"];
$artisttosearch = $_GET["artist"];
if($titletosearch !=" " && $titletosearch!=" ")
$rs = mysql_query("SELECT title,price,date,category FROM music where title like '%$titletosearch%' or category like '%$categorytosearch%' ")  or die ("invalid query");
elseif($titletosearch =="" && $titletosearch!=" ")
$rs = mysql_query("SELECT title,price,date,category FROM music where category like '%$categorytosearch%' ")  or die ("invalid query");
elseif($titletosearch !=" " && $titletosearch=="")
$rs = mysql_query("SELECT title,price,date,category FROM music where title like '%$titletosearch%' ")  or die ("invalid query");

Problem with my code is this whenever i provide title only or category only in url then it searches so many records. means it work properly if i provide both title and category. if any of them is not given then i provide so many results records.

Please help how should i handle these conditions. ?

Thank you in advance.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119

4 Answers4

6

Closest you get to english. See empty()

if (empty($_GET['title'])) { echo "It's Empty!!!"; }

Other then that, you should enforce the fulfillment of the form using the required= property in HTML5 (if you're using HTML5) and/or using JavaScript to make sure the form is valid.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

1) Instead of simply getting $_GET["key"] always wrap it in trim() method to eliminate any extra characters.

2) By default in your SQL if say any of the inputs are empty, the '%%' search will greedily match anything. You can construct your SQL statement based on inputs available and use the TOP feature; eg: SELECT TOP 100 title, price.. etc. etc. this will always limit your records returned from the backend. TOP is usually a construct provided in databases, so simply lookup the SELECT statements documentation and you should be good to go

3) If you follow 2), there will be only one mysql_query statement at the very end

Good luck

gsvolt
  • 184
  • 8
0
$w = array();
if (!empty($_GET["title"])) {
  $w[] = "title LIKE '%".mysql_real_escape_string($_GET["title"])."%'";
}
if (!empty($_GET["category"])) {
  $w[] = "category LIKE '%".mysql_real_escape_string($_GET["category"])."%'";
}
// and so on

$where = '';
if (count($w)) $where = "WHERE ".implode(' AND ',$w);
$sql = "SELECT title,price,date,category FROM music $where";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

You can use isset()

you can also check if its empty()

How to check if $_GET is empty?

Community
  • 1
  • 1
Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94