2
I have an array sent by $_POST:
array(4) {
["gabarito"]=> string(1) "4"
["resposta1"]=> string(1) "A"
["resposta2"]=> string(1) "B"
["resposta3"]=> string(1) "A"
}
I need to take this information to make an INSERT in MYSQL, but I don’t know how to get the name of $_POST (for example: "feedback"). The final result should be:
(gabarito, resposta1, resposta2, resposta3)
values
(4,A,B,A)
Since this array is variable, it can have 4 items, 10, 50, various, so it would need to be in a dynamic way
What I thought was the option below, but I do not know how to get the name of $_POST (is that how you speak? I am still a beginner, in $_POST['reposta1'] the answer1 is name? parameter?)
I did it this way:
foreach($_POST as $resposta){
$resposta.=',';
$respostas .= $resposta;
}
echo '('.$respostas.')';
This results in (4,A,B,A,)
Search by the functions
array_keys
andarray_values
, I believe it will help you. Also theimplode
, orjoin
, already ahead of time to convert array for string.– Woss
Column names in table change? n can pass
$_POST['resposta1'] ....
?– rray
the names are always the same, but sometimes it may only have answer1 and answer2 and another may have up to 150 responses
– Leandro Marzullo