user6196776
user6196776

Reputation:

How to search on multiple fields (SQL, PHP)?

I can not search on multiple fields on php. Conditions of AND and OR are not working in three fields.

Example:

$string_query = mysql_query ("SELECT * FROM Rock WHERE author ="."'".htmlspecialchars($_POST['search'])."' AND name ="."'".htmlspecialchars($_POST['name'])."' OR god="."'".htmlspecialchars($_POST['god'])."' ");

If you fill only the $_POST['search'] and $_POST['name'], the search is successful, but if $_POST['search'], $_POST['name'] and $_POST['god'] is not working. How to be?

Upvotes: 0

Views: 69

Answers (1)

ram singh
ram singh

Reputation: 140

hey you can use concatenate concept in your sql the following code will help you if any query you can ask

$var='';
$_POST['search']='adas';
$_POST['name']="ram";
$_POST['god']="shankar";
$m=2;
$n=5;
 $c=4;
if ($m==2) {
$var.= "author=htmlspecialchars($_POST[search])" ;
}
if ($n==5) {
$var.=" AND name ="."'".htmlspecialchars($_POST['name'])."' " ;
}
if ($c==4) {
$var.=" OR god="."'".htmlspecialchars($_POST['god'])."' " ;
}

echo "SELECT * FROM Rock WHERE $var ";

Upvotes: 2

Related Questions