Problems submitting multi-line forms

Asked

Viewed 54 times

1

I need a light, because, I’m days trying to understand this reasoning of Array in PHP. Follow 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 building PHP, I did it this way:

<?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 am missing?

1 answer

1


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

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 don’t see the need to insert each $_POST into a single array, whereas these $_POSTs are already an array. So I believe it’s easier for you to do so:

 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.

  • 1

    thanks. You made it clear and also solved the doubt. : D Hugs.

  • 1

    @Robson quiet... Even I was getting confused! I almost went looking for a php course for me. rsrs.. Hug!

Browser other questions tagged

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