0

I originally had this working:

url: http://server/blah.php?FacilityCode=FT

$facilitycode = mysql_real_escape_string($_GET["FacilityCode"]);
$sql = "SELECT ..." .
       "FROM ..." .
       "WHERE ..." .
       "AND ('" . $facilitycode . "' = '' OR Facility.FacilityCode = '". $facilitycode . "')";
$result = mysql_query($sql);

But I want to change this so that people can submit multiple values in the query strying somehow, ie: http://server/blah.php?FacilityCode=FT,CC,DD,EE

I tried changing the query to an "IN" clause instead of an "equals" but I'm not sure how to get the ' marks around each element.

1
  • $facilitycode = "'" . implode("', ", explode(',', mysql_real_escape_string($_GET['FacilityCode']))) . "'"; Also you should get rid of the old legacy mysql_ functions. Use PDO instead. Commented Sep 26, 2012 at 22:23

5 Answers 5

2

Use implode() function for IN (...).

$a = array('AB', 'CD', 'EF', 'ZE');
echo "field IN ('" . implode("', '", $a) . "')";

... will output:

field IN ('AB', 'CD', 'EF', 'ZE')

+escape every option you get.

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

Comments

0
$facilitycode = mysql_real_escape_string($_GET["FacilityCode"]);
$array=explode(',',$facilitycode);
foreach ($array as $a){$output.="'$a',";}
$clause=substr($output,0,-1);

Comments

0

If your trying to create a string which looks like this: 'AB', 'CD', 'EF', 'ZE'

Try this before its placed inside the query:

$facilitycode = preg_replace('/([^,]+)/', '\'$1\'', $facilitycode);

Comments

0

I wrote this based on your query, but still I dont get this part of query "AND ('" . $facilitycode . "' = ''", anyway you need to check if $_GET data have "," and if does explode that variable by "," so that you can add an OR clausule for everything that was separated by "," in $_GET data.

After that just form your query by doing a foreach for every element in exploded array like I done below:

<?php

$facilitycode = $_GET["FacilityCode"];

$facility_number_chk = strpos($facilitycode, ",");

if ($facility_number_chk > -1) {

    $facilitycode = explode(",", $facilitycode);

    $sql = "SELECT ..." .
    "FROM ..." .
    "WHERE ..." .
    "AND ('" . $facilitycode . "' = ''";

    foreach($facilitycode as $facode) {

        $facode = mysql_real_escape_string($facode);

        $sql .= " OR Facility.FacilityCode = '". $facode . "'";

    }

    $sql .= "')";

}
else {

    $facilitycode = mysql_real_escape_string($facilitycode);

    $sql = "SELECT ..." .
    "FROM ..." .
    "WHERE ..." .
    "AND ('" . $facilitycode . "' = '' OR Facility.FacilityCode = '". $facilitycode . "')";

}

$result = mysql_query($sql);

And if there is only one element in $_GET data just do an else like I done with your regular query.

2 Comments

Be more specific, I wrote that code for few minutes and didnt tested it. It is given for guiding purpose only as one of the possible directions in which this problem could be solved.
I just saw "AND ('" . $facilitycode . "' = ''";, where $facilitycode is array, so output off this will be AND ('Array' = ''";.
0

I ended up using a combination of a few of the answers. Basically I exploded on the ",", then did a foreach to add the ' marks and call escape_string, and then imploded it back.

$facilitycodes = $_GET["FacilityCode"];
if ($facilitycodes == '') {
  $additionalfilter = '';
}
else {
  $facilitycodearray = explode(",", $facilitycodes);
  foreach($facilitycodearray as &$facilitycode) {
    $facilitycode = "'" . mysql_real_escape_string($facilitycode) . "'";
  }
  $facilitycodesformatted = implode(",", $facilitycodearray);
  $additionalfilter = " AND Facility.FacilityCode IN (" . $facilitycodesformatted . ")";
}

$sql = "SELECT ..." .
"FROM ..." .
"WHERE ..." .
$additionalfilter;

Comments

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.