Searching multiple criteria PHP and MySQL

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
sbgmedia
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Apr 14, 2021 7:05 pm

Wed Apr 14, 2021 7:40 pm

Hello,

I have a standard code to search by column and am now trying to add the option of searching multiple criteria in one column.
Currently I can search by one criteria, so if the user selects QLD it will display all QLD entries, what changes do i need to make to enable the user to search both for QLD and NSW?

I have updated the <select> element to be multiple, but am not sure how to adjust the PHP and MySQL to process multiple criteria.

Can someone help?

Thanks,
sbgmedia

Code: Select all

	<?php
	$condition	=	'';
	if(isset($_REQUEST['Location']) and $_REQUEST['Location']!=""){
		$condition	.=	' AND Location LIKE "%'.$_REQUEST['Location'].'%" ';
	}
	
	
	$userData	=	$db->getAllRecords('candidates','*',$condition,'ORDER BY id ASC');
	?>


<select name="Location[]" id="Location" class="form-control" value="<?php echo isset($_REQUEST['Location'])?$_REQUEST['Location']:''?>" multiple>
  <option value="" <?php if(isset($_REQUEST['Location']) && $_REQUEST['Location'] == '') 
          echo ' selected="selected"';?>></option>
  <option value="ACT" <?php if(isset($_REQUEST['Location']) && $_REQUEST['Location'] == 'ACT') 
          echo ' selected="selected"';?>>ACT</option>
  <option value="NSW" <?php if(isset($_REQUEST['Location']) && $_REQUEST['Location'] == 'NSW') 
          echo ' selected="selected"';?>>NSW</option>
   <option value="QLD" <?php if(isset($_REQUEST['Location']) && $_REQUEST['Location'] == 'QLD') 
          echo ' selected="selected"';?>>QLD</option>
</select>
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1222
Joined: Mon Feb 22, 2016 5:52 pm

Fri Apr 16, 2021 9:39 am

Use the OR operator:

Code: Select all

WHERE
  state = 'QLD' 
OR
  state = 'NSW'
MySQL OR
Post Reply