array to string conversion error

Asked

Viewed 174 times

0

I need to insert in the variable database that are in a php array, I wrote the cogido:

    for ($i=1; $i <= 20 ; $i++) { 
    $sqlp= "INSERT INTO pergunta$i (pergunta, opcao1, valor1, opcao2, valor2, opcao3, valor3, opcao4, valor4, opcao5, valor5, opcao6, valor6, opcao7, valor8, opcao2, valor8) VALUES (`$tabela[$i][1]`,`$tabela[$i][2]`,`$tabela[$i][3]`,`$tabela[$i][4]`,`$tabela[$i][5]`,`$tabela[$i][6]`,`$tabela[$i][7]`,`$tabela[$i][8]`,`$tabela[$i][9]`,`$tabela[$i][10]`,`$tabela[$i][11]`,`$tabela[$i][12]`,`$tabela[$i][13]`,`$tabela[$i][14]`,`$tabela[$i][15]`,`$tabela[$i][16]`,`$tabela[$i][17]`,)";
        $salvar= mysqli_query($conexao, $sqlp);
}

when executed, the browser returns array to string conversion error, as I can resolve?

  • Because then, if the error is in converting array to string vc should post that part of the code as well

  • error says conversation is on this line

  • table name question$i ?? 20 tables?

  • yes, there are 20 tables with names: question1, question2, question3... up to 20, so the idea is to use the for to vary between these tables

  • right, but there must be some invalid value in the conversion. I only see there

  • invalid value in what sense?

  • The arrays in php, when inside double quotes, concatenates as follows:"array: {$array['key']}" where the keys indicate that inside is an array.

Show 2 more comments

2 answers

1

Just as the use of the comma is a recurring problem for the written language user, in programming the same thing occurs.

There’s a comma left in , $tabela[$i][17],)

remove the comma before closing the parentesis $tabela[$i][17])

THE FATAL COMMA

The Russian Tsarina Maria Fyodorovna once saved a man’s life, only changing the comma of her sentence. Very clever, she who did not agree with the decision of her husband, Alexander II, used the following device.

The Tsar sent the prisoner to prison and death in the Siberian dungeon.

At the end of the arrest warrant it was written: "Forgiveness impossible, send to Siberia"

Mary ordered them to draft a new order, and pretending to read the original document, changed a comma, transforming the order into: "Sorry, impossible to send to Siberia" and the prisoner was released.

That’s it. Watch out for her. Or lack of her.

0

Normally, this error occurs when you try to directly insert an array type variable into the query. This is because the database does not accept this type of data. To save an array to a record you first need to convert it into a string by placing a separator that can be any character:

$arrayasergravado = implode ( 'separator' , $array );

So your array will be recorded in the database. In your case it is difficult to say what the error problem is because it should be known how you built this array that you are trying to save. he is two-dimensional.

Browser other questions tagged

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