Do the INSERT of an Array, regardless of the amount of $ _POST

2

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

It can add 4 images, so you can add 10,15,1,3 and so on.

How would you look for QUERY ?

Form POST Array:

array (

[IMG1] => /img/nomedaimagemquefoiupload1.jpg

[IMG2] => /img/nomedaimagemquefoiupload2.jpg

[IMG3] => /img/nomedaimagemquefoiupload3.jpg

);

$ArrayDeUmPostComum = $_POST;
INSERT INTO TABELA VALUES ($ArrayDeUmPostComum)
    
asked by anonymous 12.07.2015 / 01:59

1 answer

5

If I get it right, you want to insert multiple rows into the table. In MySQL you can do this:

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

To mount this query in PHP, considering the use of PDO, and that in its $_POST not only the paths of the 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);
    
12.07.2015 / 02:04