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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you rikudo sir...your help really imaging for me...Thank you so much...have a great life...
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

Comments

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

Comments

-1

You can use isset()

you can also check if its empty()

How to check if $_GET is empty?

1 Comment

-1 isset() will be true if the url is example.com/index.php?title=

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.