Update with Multidimensional Array

Asked

Viewed 429 times

-3

I’m having a terrible problem, I can’t update mysql via PDO, with array that comes from some form fields. I’ve tried a lot and so far I can’t update. The array comes in this format.

Array
(
    [id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
        )

    [url] => Array
        (
            [0] => /ckfinder/userfiles/images/2141.jpg
            [1] => rty
            [2] => rtyrcial.jpg
            [3] => rtyrtrfiles/images/3.jpg
            [4] => rtyrimages/especial.jpg
            [5] => rtyrfiles/images/especial.jpg
            [6] => rtyres/images/especial.jpg
            [7] => /ckfinder/userfiles/images/3.jpg
            [8] => /ckfinder/userfiles/images/Capa1.jpg
            [9] => asd
        )

)

TABLE (for example data): inserir a descrição da imagem aqui

SQL (PDO):

UPDATE via_imagens SET img_url = array[url] WHERE img_capa IS NULL AND img_id = array[id]

OK I’m passing like this:

$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');

$sql = 'UPDATE via_imagens SET img_url = ? WHERE img_capa IS NULL AND img_id = ?'; 

$stmt = $db->prepare($sql);

foreach($dados as $item ){
  $stmt->execute(array($dados['url'], $dados['id']));
}

But this mistake is coming: Notice: Array to string Conversion in, pointing to the line of $stmt->execute();

  • 5

    -1 by the title "help/urgent" and the unnecessary use of the high-end

  • What have you tried to do so far José? How is the excerpt of the code that is in trouble. I also recommend [Edit] your text. Read more at [Ask]

  • I can’t loop both array[id] and array[url]. values into a single loop. To using foreach. And another, when I pass the value in array format to the PDO statement I get the Notice: Array to string Conversion.

  • I changed my answer.

1 answer

0

The simplest way to do this is to take the array values pass them by a for and run the update within it. If you need something more restrictive can a transaction to ensure the integrity of the operation.

Notice: Array to string Conversion

It happens when you try to print an array value where the call of the indexes is not correct, what will happen is to save the value Array in the database.

From the point your array is in a called variable $lista your access should be done so: $lista['url'][1] will be displayed the value rty, the index 1 should be change a counter($i for example).

$dados = array(); //esse array é tem a url e o id da sua tabela no exemplo

$db = new PDO('mysql:host=localhost;dbname=teste', 'usuario', 'senha');

$sql = 'UPDATE via_imagens SET img_url = ? WHERE img_capa IS NULL AND img_id = ?';
$stmt = $db->prepare($sql);

$total_itens = count($dados['id']);

for($i=0; $i<$total_itens; $i++){
   $stmt->execute(array($dados['url'][$i], $dados['id'][$i]));
}

Browser other questions tagged

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