Insert an Array, regardless of the amount of $_POST

Asked

Viewed 396 times

2

For example, a person adds many Images to be published.

She can add 4 images as well as can add 10,15,1,3 and etc.

How would the QUERY?

Form POST array:

array (

[IMG1] => /img/nomedaimagemquefoiupload1.jpg

[IMG2] => /img/nomedaimagemquefoiupload2.jpg

[IMG3] => /img/nomedaimagemquefoiupload3.jpg

);

$ArrayDeUmPostComum = $_POST;
INSERT INTO TABELA VALUES ($ArrayDeUmPostComum)
  • Are you using any driver that allows you to use PREPARE, ie PDO or mysqli?

  • I use PDO, but my codes are on another PC that gave pal, I will have to buy a HD player... But any example enough

1 answer

5


If I understand correctly, you want to insert several rows in the table. In Mysql you can do so:

INSERT INTO tabela
    (caminho)
VALUES
    ('...'),
    ('...'),
    ('...');

To build this query in PHP, considering the use of PDO, and that in your $_POST not only the paths of images come:

$quantidade = count($_POST);
$parametros = array_fill(0, $quantidade, '(?)');
$valores = array_values($_POST);
$sql = 'INSERT INTO tabela (url) VALUES ' . implode(',', $parametros);
$query = $dbh->prepare($sql);
$query->execute($valores);
  • That’s right, but how do I turn $Array into multiple VALUES? DROPBOX for example, you can do a DRAG file that will make the INSERT in your database

  • 1

    Edit the question and show how you receive the data in the POST, and how is the structure of your table. It’s hard to understand exactly what you want, I thought it was the query structure...

  • 1

    See if my update helps.

  • Well, I can’t test it now, but I bet it will help me, it’s a little different from the PDO I usually use, because in $_POST it’s :nomedopost, :nomedopost2, and so on, that changes something?

  • No, the difference is that with ? you don’t need to name the parameters. This code of mine considers that you only have a list of paths, so you don’t have to keep naming them one by one.

  • Hmmm, I understood, basically what I wanted was this, to make the INSERT of an Array independent of the amount of $_POST, thank you @bfavaretto, as soon as I get my files back from hard drive I will update my files!!!

  • Just another question here, this will create different dates on the table right? Because as in the example, the url column would go http://minhaurl/img/img1.jpg,http://minhaurl/img/img2.jpg,http://minhaurl/img/img3.jpg,

  • Will create a line by way.

  • So?: http://notepad.cc/becsuwa82

  • Yeah, that’s how it is .

  • But to include id, use array_fill(0, $quantidade, '(?, 1)'); (and include the column name in the first half of the query)

  • You will need to adapt, you may need to loop in hand to assemble the query.

  • 1

    oks, array_fill(0, $quantidade, '(? , $publication_id, $usuario_ID)');

  • Use double quotes in the string inside, otherwise the variables will not be interpolated.

  • okay;;;;;;;;;;;;;

Show 10 more comments

Browser other questions tagged

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