1

I am sending a variable with multiple values like this:

JQUERY:

 $(".indexMain").load('indexMain.php?color=' + colors.join("+"), function() 

--> indexMain.php?colors=blue+red+brown

I want to _GET those values and then use them in a while loop to put them into a SQL query like this one:

$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); $items -> bindValue(":colorbase1", $color); 

Thanks!

EDIT: here is my current code but it is not working, it just shows the items corresponding to the 1st color.

foreach (explode(' ', $_GET['color']) as $color) { $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); $items -> bindValue(":colorbase1", $color); } 
4
  • 1
    That parameter only has a single value, "blue red brown". Commented Aug 17, 2012 at 3:02
  • You can't bind more than one value to one "variable" in PDO. Commented Aug 17, 2012 at 3:05
  • @Tyler, he said that he wanted to use them in a while loop, so I am guessing that he intends to use them one at a time. I could be wrong though :-P. Commented Aug 17, 2012 at 3:05
  • What does var_dump(explode(' ', $_GET['color'])); output? Commented Aug 17, 2012 at 19:01

3 Answers 3

3
indexMain.php?color=blue+red+brown 

should be

indexMain.php?colors%5B%5D=blue&colors%5B%5D=red&colors%5B%5D=brown 

which is equivalent to

indexMain.php?colors[]=blue&colors[]=red&colors[]=brown 

This creates an array accessible with $_GET['colors']. After that use this PHP:

foreach ($_GET['colors'] as $color) { $query = $con->prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); if ($query->execute(":colorbase1", $color)) { // use results from query } else { // handle failed query } } 

If you don't want to change the query string, you can do this alternatively:

foreach (explode(' ', $_GET['colors']) as $color) { $query = $con->prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); if ($query->execute(":colorbase1", $color)) { // use results from query } else { // handle failed query } } 

Note that the only change is in the first line and we are splitting the string by spaces (because "+" gets converted to a space character).

Also note that both examples assume that $_GET['colors'] is defined. You can use isset() to check if it is defined.

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

2 Comments

Hi, thanks this looks good but doesn't work in my code... I am editing my original post with the current code... Thanks!
Well, you're not using the code that I provided. You're binding a variable but not doing anything with it. You have to execute the query like I did.
0

I think this should work:

$colors = explode(' ', $_GET['color']); foreach($colors as $color) { // your code here... } 

1 Comment

Thanks, I updated my code on the top following your idea but it doesn't work yet... would you mind taking a look? It's edited
0

Try this

$colors = explode("+", $_GET['color']); foreach($colors as $color){ $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); $items -> bindValue(":colorbase1", $color); } 

How about this

$colors = explode(" ", $_GET['color']); foreach($colors as $color){ $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); $items -> bindValue(":colorbase1", $color); } 

4 Comments

wouldn't + in a query parameter be converted to spaces?
This will not work, $_GET['color'] will contain spaces, not the '+' sign that the GET parameter would have been encoded with.
@EvanTeran please check the latter one . .
@Geoffrey please check the latter one . .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.