How to get all $_POST information

Asked

Viewed 593 times

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 and array_values, I believe it will help you. Also the implode, or join, already ahead of time to convert array for string.

  • Column names in table change? n can pass $_POST['resposta1'] .... ?

  • the names are always the same, but sometimes it may only have answer1 and answer2 and another may have up to 150 responses

3 answers

3


You thought well, just wanted 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 problem of SQL Injection, which can be easily solved with real_escape_string

Another solution, which I think best is to use php’s native functions:

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

Important:

Although it works to use several columns in this way is not very recommended, I suggest creating another table that relates to the one that already exists. I suggest you take a look at the Normal forms

  • 1

    perfect, gave right!

  • Take a look at my last paragraph and especially the linked question

0

Well, you thought well of using the foreach, but at the same time you are pulling a array, then I recommend you do so:

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

Having its variable $_POST this way, it will be easier for you now to assemble what you really want, in which case it is to show the answer of the feedback in question. We’ll send it to the foreach your feedback:

$_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 possess this result:

gabarito = A, B, C

Another method already putting in the INSERT INTO would be so:

$_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 look:

$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.')';

For thus, you set up your foreach and its array In your own way, now if you use code with only one standard style, it will take longer for you to adapt or transfer the method. I hope I’ve helped!

-1

Use foreach:

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

it will iterate upon ALL the key to $_POST

Edit: you’re right, the above code didn’t work

  • gave an error: Warning: Illegal string offset 'feedback' in ...

  • @Andersoncarloswoss you’re right, I was traveling in mayonnaise, I tidied up the code, the right is $_POST[$key] being $value the value of the variable, sorry the gaffe

  • Instead of "interact", I think the right thing would be "iterate", nay?

  • I blame my broker entirely, but you’re right, I appreciate

  • 1

    Your answer may answer the question, but this very vague, try to be more complete

Browser other questions tagged

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