receive form with var_dump

1

Now, I'm sending form with several POST , I get all of them and I add them to the DB. But I'm seeing a method using var_dump , but I do not know how to deploy it in my code.

An example of how I do it today:

            <?php
        if ((!empty($action)) and ( $action == "envia")) {

            // Recebe dados
            $nome1 = filter_input(INPUT_POST, 'nome1', FILTER_DEFAULT);
            $nome2 = filter_input(INPUT_POST, 'nome2', FILTER_DEFAULT);

            // Adino no BD mysql
            $mysqli->query("INSERT INTO nomes VALUES ('0', '$nome1')");
            $mysqli->query("INSERT INTO nomes VALUES ('0', '$nome2')");
        }
        ?>

        <form name="form" method="post" action="index.php?action=envia">
            <input  type='text' name='nome1'>
            <input  type='text' name='nome2'>      
            <button type='submit'>Enviar</button>
        </form>

Is there any way I can simplify this with var_dump . This is a simple example, as I have a form with about 30 POST .

    
asked by anonymous 06.07.2016 / 19:01

1 answer

3

I did not test because my php is not recognizing filter_input , but so should I get:

<?php
// povoando post (para teste)
$_POST['nome1'] = "o";
$_POST['nome2'] = "v";
$_POST['nome3'] = "e";
$_POST['nome4'] = "r";
$_POST['nome5'] = "f";
$_POST['nome6'] = "l";
$_POST['nome7'] = "o";
$_POST['nome8'] = "w";

var_dump($_POST);

$valores = array();
foreach($_POST as $key => $value)
    array_push($valores, filter_input(INPUT_POST, $key, FILTER_DEFAULT));

var_dump($valores);

foreach($valores as $value)
    $mysqli->query("INSERT INTO nomes VALUES ('0', '$value')");

As quoted in the comments, var_dump is only for display, according to documentation :

  

var_dump - Shows information about the variable

     

This function will show a structured representation of one or more expressions, including   the type and the value. Arrays and objects are recursively exploited with   values indented in the structure shown.

That is, it is used only to test the system, see the values of its variables.

In the code I used a foreach to loop through the $ _POST data, thus doing data filtering and putting the result of the filtering into a list, after that, I have another foreach that will execute in mysqli . The same could be done with:

foreach($_POST as $key => $value)
{
    $value = filter_input(INPUT_POST, $key, FILTER_DEFAULT);
    $mysqli->query("INSERT INTO nomes VALUES ('0', '$value')");
}

Or also:

foreach($_POST as $key => $value)
    $mysqli->query("INSERT INTO nomes VALUES ('0', '" . filter_input(INPUT_POST, $key, FILTER_DEFAULT) . "')");
    
06.07.2016 / 19:42