How to capture (GET) multiple values sent in the same variable (PHP)?

Asked

Viewed 1,243 times

0

After a query, I am sending different values through the same variable (through an input):

 <input type="hidden" id="sku" name="sku" value="<?= $reg->sku ?>"> 

This way, my URL is as follows:

http://localhost/admin/anuncios/relatorios/exportado_confirm.php?sku=BL001_BA307&sku=BL001_BA308&sku=BL001_BA667&sku=BL001_BA668&sku=BL001_BA672&sku=BL003_BA309&sku=BL005_BA27&sku=BL166_BA7&sku=BL166_BA8&sku=BL178_BA310&sku=BL184_BA1&sku=BL184_BA4&sku=BL184_BA669&sku=BL202_BA5

But I can’t then capture all the "sku" sent through the $sku = $_GET['sku']);

'Cause when I run the next round UPDATE: $sql = "update anuncios set exportado = '1' where sku = '{$sku}'";

It can only capture the last value sent in the "sku" variable and run the UPDATE in that last "sku" listed in the URL.

I wish I could capture all the values, and this UPDATE rotate (while) while there is some value in the sku variable. Does anyone have any suggestions to help me?

Note: I’m actually doing everything by POST, just put by GET to be visible the values sent in the same variable.

  • 3

    Try to define the field input with name="sku[]", adding the brackets. Thus, I believe that $_GET["sku"] will be a array, but remember to convert it properly to string before using in SQL query.

  • 1

    Do you know how to use the explode? It might help. $skus = explode('&', $_GET); after that just give a Count(skus) to know the number of values returned

  • a little confusion that there, starts with get and in the end becomes post and is only an input, if it is only an input for that bracket in name?

1 answer

3


The official structure of the URL is defined by RFC 2986, Session 3.4 addressing the definition of query and even in it there is no "official" way to pass multiple values to the same parameter. Being the query an opaque value - that is, its real meaning depends on the implementation that will parse the URL, the way to pass multiple values will vary.

In PHP, brackets are officially used after the field name:

<input type="hidden" id="sku" name="sku[]" value="BL001_BA307"> 
<input type="hidden" id="sku" name="sku[]" value="BL001_BA308"> 
<input type="hidden" id="sku" name="sku[]" value="BL001_BA667"> 

This will generate a URL similar to:

//localhost?sku[]=BL001_BA307&sku[]=BL001_BA308&sku[]=BL001_BA667

And PHP, when analyzing it, will generate a array with such information:

var_dump($_GET["sku"]);

array(3) {
  [0]=>
  string(11) "BL001_BA307"
  [1]=>
  string(11) "BL001_BA308"
  [2]=>
  string(11) "BL001_BA667"
}

So you can use all the information next to SQL, remembering that it should be properly converted to string before using it in the SQL query.

This technique will work for both GET and POST requests. The only difference, in fact, between them will be the way the values will be passed via HTTP request: in GET is via URL and in POST is via the body of the request.

As in the SQL query, the intended result is something like:

select ... where sku in ("BL001_BA307", "BL001_BA308", "BL001_BA667");

What you can do is use implode of PHP:

$sku = implode(",", array_map(function ($item) {
    return sprintf('"%s"', $item);
}, $_GET["sku"]));

// $sku = '"BL001_BA307", "BL001_BA308", "BL001_BA667"';

And in SQL:

select ... where sku in ({$sku});
  • 1

    @Leocaracciolo not knowing the code that treats these Urls is difficult to say, but the presence of the brackets makes PHP understand that it is the same field, thus converting to a array in $_GET["sku"] (or POST). Without brackets, PHP will only take the last value.

  • I know that, I’m just not understanding why the brackets are just an input, or maybe I’m wrong and must be several inputs ..

  • 1

    @Leocaracciolo by the structure of the code it is possible to assert that are multiple fields, because $reg tends to be an object that is iterating the results of a database query. That is, there will be a field for each record. The author himself begins by saying "after a query..." :D

  • thanks Anderson, my bad is that I take Portuguese to the letter :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.