How to get all information from $ _POST

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 get this information to do an INSERT in MYSQL, but I do not know how to get the $ _POST name (for example: "template"). The end result should be:

(gabarito, resposta1, resposta2, resposta3)
values
(4,A,B,A)

Given that this array is variable, it can have 4 items, 10, 50, several, so it would need to be dynamically

What I thought was the option below, but I do not know how to get the name of $ _POST (is that how it talks? I'm still a beginner, in $ _POST ['answer1'] the answer1 is name? parameter?)

I did this:

foreach($_POST as $resposta){
    $resposta.=',';
    $respostas .= $resposta;
}
echo '('.$respostas.')';

Then this results in (4, A, B, A,)

    
asked by anonymous 09.10.2018 / 20:38

3 answers

3

You thought about it, you just needed to add the keys:

$_POST = [
    "gabarito" => "4",
    "resposta1" => "A",
    "resposta2" => "B",
    "resposta3" => "A"
];

$into = "";
$values = "";

foreach ($_POST as $chave => $valor) {
    $into .= "$chave, ";
    $values .= "'$valor', ";
}

//Retira o último ', '
$into = trim($into, ", ");
$values = trim($values, ", ");

echo "INSERT INTO ($into) VALUES ($values)";

But this code has the SQL Injection problem, which can be easily solved with real_escape_string

a>

Another solution that I think is best to use the native php functions:

echo "INSERT INTO tabela (".implode(", ", array_keys($_POST)).") VALUES ('".implode("', '", $_POST)."')";

Important:

Although it works to use multiple columns in this way is not very recommended, I suggest creating another table that is related to the one that already exists. I suggest taking a look at Normal Forms

    
09.10.2018 / 21:19
0

Well, you've thought twice about using foreach , but at the same time you're pulling from a array , so I recommend doing so:

$_POST = array('gabarito' => array('resposta1' => 'A', 'resposta2' => 'B', 'resposta3' => 'C'));

Having your variable $_POST in this way, it will be easier for you now to mount what you really want, which in the case is to show the response of the template in question. We will send the template to foreach :

$_POST = array('gabarito' => array('resposta1' => 'A', 'resposta2' => 'B', 'resposta3' => 'C'));
foreach ($_POST as $key => $value) {
    $data = "{$key} = ".implode(', ', $value);
}
echo $data;

Well, I would do so, automatically you will have this result:

gabarito = A, B, C

Another method already putting in INSERT INTO would look like this:

$_POST = array('gabarito' => array('resposta1' => 'A', 'resposta2' => 'B', 'resposta3' => 'C'));
foreach ($_POST as $key => $value) {
    $questao = $key;
    $respostas = implode(', ', $value);
}
echo 'INSERT INTO tabela (ID, GABARITO, RESPOSTA1, RESPOSTA2, RESPOSTA3) VALUES (NULL, '.$questao.', '.$respostas.')';

Well, an example of how to do this in practice is so looks:

$quest = 4;
$resp = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$_POST = array($quest => array('resposta1' => $resp['a'], 'resposta2' => $resp['b'], 'resposta3' => $resp['c']));
foreach ($_POST as $key => $value) {
    $questao = $key;
    $respostas = implode(', ', $value);
}
echo 'INSERT INTO tabela (ID, GABARITO, RESPOSTA1, RESPOSTA2, RESPOSTA3) VALUES (NULL, '.$questao.', '.$respostas.')';

Well, you set your foreach and array your way, now if you use a code with only a default style, it will take longer for you to adapt or transfer the method. I hope I have helped!

    
09.10.2018 / 21:33
-1

Use foreach:

# v2
foreach ($_POST as $key => $value) {
    echo $value;
}

It will iterate over ALL the $ _POST key

Edit: you're right, the above code did not work

    
09.10.2018 / 20:43