Save form attribute without defined name

1

I'm having a problem with a php script. The name attribute of a page receives a value that is generated by an array. What I need to do is basically generate a table in which each row has a button that serves to delete the same information received from the database, this button is which I use the attribute name to save the id of the received record. So, I do not know what I put on the other php page so that this value is saved. I'll put the code below for further analysis.

Below the php page that shows the elements on the screen.

<?php
        $query = sprintf("SELECT conteudo, autor, titulo, pagina, id FROM atualizacoes ORDER BY id DESC");

        $dados = mysql_query($query, $con) or die(mysql_error());

        $linha = mysql_fetch_assoc($dados);

        $total = mysql_num_rows($dados);
        ?>
                <body>
            <div class="row ">
                <div class="col-md-8 col-md-offset-2">
                    <form name="login" method="post" action="../removendo.php">
                <h2 style="color: white">Atualizando blog</h2>

            <table class="tabelaconteudo">

                        <?php
                        if($total>0){
                            do{

                                ?>
                                <tr>
    '<td><?= $linha['titulo']?></td>
    <td><?= $linha['pagina'] ?></td>
    <td><?= $linha['autor'] ?></td>
    <td><input type="submit" class="btn-cad" style="color: white" value="Apagar"
name="ref_<?= $linha['id'] ?>"></td> //AQUI É O CAMPO O QUAL NÃO CONSIGO SALVAR A INFORMAÇÃO
    </tr>


                            <?php

                            }while ($linha = mysql_fetch_assoc($dados));

                        }
                        ?>

            </table>
                    </form>

            </div>
            </div>
            </body>

Below the php page that loads when you press the submit button (the same as I can not set a specific name).

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
    $id=$_POST['NAME QUE NAO ESTA DEFINIDO'];
    $restid = substr($id, 1, 4);
    $sql = mysql_query("DELETE FROM atualizacoes WHERE id = $restid");
    echo"
<script>
document.getElementById('loader').onpageshow = alertfunc();
function alertfunc() {
    alert('Ocorreu algum erro.'$sql);
    }
</script>

";
?>

I started with web programming a short time ago, and everything I've learned with php so far has been this. I've swept the entire internet behind this and never found something concrete so far, then in desperation I come here for help. From now on I thank all those who were willing to help.

    
asked by anonymous 28.10.2016 / 14:54

2 answers

0
<td>
  <form name="login" method="post" action="../removendo.php">
    <input type="hidden" value="Apagar" value="ref_<?= $linha['id'] ?>" name="campo_para_apagar" />
    <input type="submit" class="btn-cad" value="Apagar" name="submit" />
  </form>
</td>

If you only submit these values one at a time, one solution is to open a <form> different for each row in the table, with a hidden input that has its value. So when you click Delete it will only send the value of that input and it will be available in the request as

$id = $_POST['campo_para_apagar'];
    
28.10.2016 / 15:21
0

You can create an array in the name attribute like this:

<input type="submit" class="btn-cad" style="color: white" value="Apagar"
name="ref[<?= $linha['id'] ?>]">

and in PHP get the ID like this:

$id = key($_POST['ref']);
    
28.10.2016 / 15:52