Jump to content

Inserting AND?


pazafuera

Recommended Posts

Hello,  I've got a SQL query that is being built on the fly in PHP (with which I am very new).  I've got 4 different arrays that are built based on what the user inputs into the form.  Inside each array, the phrase 'OR' is added between each array value.  What I need to do, but can't seem to figure out, is insert 'AND' inbetween each array IF AND ONLY IF the array before it actually has value.

 

I hope that makes sense...

 

    if ($_POST[searchKeywords]<>"")
	{
  			$query = "(jobtitle LIKE '%".mysql_real_escape_string($_POST[searchKeywords])."%' OR employer LIKE '%".mysql_real_escape_string($_POST[searchKeywords])."%' OR excerpt LIKE '%".mysql_real_escape_string($_POST[searchKeywords])."%' OR description LIKE '%".mysql_real_escape_string($_POST[searchKeywords])."%')";
	} 

$queryAry = array();

   if ($_POST[Amateur]<>"")
	{
  			$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Amateur])."'";
	}

if ($_POST[Corporate]<>"")
  		{
    		$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Corporate])."'";
  		}

if ($_POST[Facilities]<>"")
  		{
    		$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Facilities])."'";
		}

if ($_POST[Health]<>"")
  		{
    		$queryAry[] ="jobsegment = '".mysql_real_escape_string($_POST[Health])."'";
  		}

if ($_POST[Professional]<>"")
  		{
    		$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[Professional])."'";
  		}

if ($_POST[sporting]<>"")
  		{
    		$queryAry[] = "jobsegment = '".mysql_real_escape_string($_POST[sporting])."'";
  		}

if ($query<>"" AND $queryAry<>"")
  		{
    		$query = $query." AND ".implode(' OR ', $queryAry);
  		}

$queryAryCategory = array();


  	if ($_POST[sales]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[sales])."'";
  		}

  	if ($_POST[Media]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[Media])."'";
  		}

  	if ($_POST[Recreation]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[Recreation])."'";
  		}

  	if ($_POST[Finance]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[Finance])."'";
  		}

  	if ($_POST[Facilities]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[Facilities])."'";
  		}

  	if ($_POST[Website]<>"")
  		{
    		$queryAryCategory[] = "jobcategory = '".mysql_real_escape_string($_POST[Website])."'";
  		}	

if ($query<>"" AND $queryAryCategory<>"")
  		{
    		$query = $query." AND ".implode(' OR ', $queryAryCategory);
  		}

$queryAryPostition = array();

  	if ($_POST[FullTime]<>"")
  		{
    		$queryAryPostition[] = "position = '".mysql_real_escape_string($_POST[FullTime])."'";
  		}

  	if ($_POST[PartTime]<>"")
  		{
    		$queryAryPostition[] = "position = '".mysql_real_escape_string($_POST[PartTime])."'";
  		}

  	if ($_POST[internship]<>"")
  		{
    		$queryAryPostition[] = "position = '".mysql_real_escape_string($_POST[internship])."'";
  		}

if ($query<>"" AND $queryAryPosition<>"")
  		{
    		$query = $query." AND ".implode(' OR ', $queryAryPosition);
  		}

$queryAryLocation = array();

if ($_POST[Location]="Region")
{
  		if ($_POST[West]<>"")
  			{
    			$queryAryLocation[] = "jobregion = '".mysql_real_escape_string($_POST[West])."'";
  			}
    
  		if ($_POST[Central]<>"")
  			{
    			$queryAryLocation[] = "jobregion = '".mysql_real_escape_string($_POST[Central])."'";
  			}
    
  		if ($_POST[Northeast]<>"")
  			{
    			$queryAryLocation[] = "jobregion = '".mysql_real_escape_string($_POST[Northeast])."'";
  			}
    
  		if ($_POST[southeast]<>"")
  			{
    			$queryAryLocation[] = "jobregion = '".mysql_real_escape_string($_POST[southeast])."'";
  			}
    
  		if ($_POST[Canada]<>"")
  			{
    			$queryAryLocation[] = "jobregion = '".mysql_real_escape_string($_POST[Canada])."'";
  			}
}

if ($_POST[Location]="State")
{
  		if ($_POST[state]<>"")
  			{
    			$queryAryLocation[] = "jobstateabbrv = '".mysql_real_escape_string($_POST[state])."'";
  			}
}

if ($query<>"" AND $queryAryLocation<>"")
  		{
    		$query = $query." AND ".implode(' OR ', $queryAryLocation);
  		}

 

The code above is inserting an 'AND' inbetween each array even if the array is empty.  What's going wrong?

 

Thanks so much in advance.

Link to comment
https://forums.phpfreaks.com/topic/102165-inserting-and/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.