Problems submitting multi-line forms

1

I need a light, therefore, I am trying to understand this reasoning of the Array in PHP. Here's my headache:

<div class="content">
<form action="" method="POST">
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Nome</th>
                <th>Sobrenome</th>
                <th>OBS</th>
            </tr>
        </thead>
        <tbody>
        <?php 
            $sql = mysqli_query($conexaoDados, "SELECT * FROM usuariosBD ORDER BY id DESC");
            if(mysqli_num_rows($sql)) {
                while($row = mysqli_fetch_array($sql)) {
        ?>
            <tr>
                <td><input type="text" name="key[]" value="<?php echo $row['id']; ?>" /></td>
                <td><input type="text" name="nome[]" value="<?php echo $row['nome']; ?>" /></td>
                <td><input type="text" name="subnome[]" value="<?php echo $row['sobrenome']; ?>" /></td>
                <td><input type="text" name="obs[]" value="<?php echo $row['obs']; ?>" /></td>
            </tr>
        <?php 
            }}
        ?>
        </tbody>
    </table>
    <button type="submit" name="submiting">Salvar dados</button>
</form>

In the example above, I will have this data:

            <tr>
                <td><input type="text" name="key[]" value="123" /></td>
                <td><input type="text" name="nome[]" value="Luiz" /></td>
                <td><input type="text" name="subnome[]" value="Aurelio" /></td>
                <td><input type="text" name="obs[]" value="Usuário premium a partir de Setembro." /></td>
            </tr>
            <tr>
                <td><input type="text" name="key[]" value="124" /></td>
                <td><input type="text" name="nome[]" value="Amós" /></td>
                <td><input type="text" name="subnome[]" value="Bernadinho da Silva" /></td>
                <td><input type="text" name="obs[]" value="Usuário comum a partir de Janeiro." /></td>
            </tr>

When I set up PHP, I did this:

<?php
if(isset($_POST['submiting'])) {
    $inputs = array(
    'ID'    =>  $_POST['key'],
    'NOME'  =>  $_POST['nome'],
    'SUB'   =>  $_POST['subnome'],
    'OBS'   =>  $_POST['obs']
    );

    foreach($inputs as $row) {
        echo $row['ID'];
        echo $row['NOME'];
        echo $row['SUB'];
        echo $row['OBS'];
    }
}

? >

Unfortunately, it does not return anything, since they exist. Could someone explain to me where I'm going wrong?

    
asked by anonymous 18.04.2018 / 14:20

1 answer

2

The problem is that you are assigning the key to the VALUE. There is no such key in this value. Your key would have to be assigned to the $input array. See:

print_r($inputs["ID"]); // isso funciona

foreach($inputs as $key => $row) {
    print_r($row[$key]); // isso não funciona
    print_r($row["ID"]); // isso não funciona
    print_r($inputs[$key]); // isso funciona
}

I do not see the need to insert every $ _POST into a single array, since those $_POSTs are already an array. So I think it's easier for you to do this:

 if(isset($_POST['submiting'])) {

    for($x = 0; $x < count($_POST['key']); $x++){
       echo $_POST['key'][$x];
       echo $_POST['nome'][$x];
       echo $_POST['subnome'][$x];
       echo $_POST['obs'][$x];
    }
}

You can do this:

if(isset($_POST['submiting'])) {
    $inputs = array(
        'ID'    =>  $_POST['key'],
        'NOME'  =>  $_POST['nome'],
        'SUB'   =>  $_POST['subnome'],
        'OBS'   =>  $_POST['obs']
    );


    foreach($inputs as $row) {
        foreach($row as $r){
            echo $r."<br>";
        }
    }
}

But I think it's redundant.

    
18.04.2018 / 15:07