Save more than one PHP and MYSQL record

1

How do I give an update on more than one record, for example it appears in the notes.php page the following information:

Thisinformationisfromabank,soeachstudentisidsdifferent.MyproblemiswhenIclickthesavebutton,becausewhenIclickititonlysavesthelastitem.WhatcanIdointhiscase?

Usedcode

<formmethod="GET" action="notas.php">
<input type='text' placeholder='Nota' name='nota' id='nota' value=" .$linha['nota_valori'] . ">
<button type="submit">Salvar</button>
</form>

No notes.php has:

$id = $_POST['id'];
$nota = $_POST['nota'];
$sql="UPDATE 'nota' SET nota_valori='$nota' WHERE nota_id=$id";
$executa= mysql_query($sql,$con) or die(mysql_error());
echo "Registro salvo com sucesso";
    
asked by anonymous 08.05.2018 / 22:31

1 answer

2

You should transform your form fields to a array and store the id in a hidden field. for example:

<input type='hidden' name='id[]' value='<?php echo $id ?>'>
<input type='text' name='nota[]' value='<?php echo $nota?>'>

And in the form's php when you get the data, just walk through a foreach and treat the data the way you need it.

<?php

$data = isset($_POST) ? $_POST : [];

foreach($data['id'] as $key => $value){

        $id = $value;
        $nota = $data['nota'][$key];

        $sql="UPDATE 'nota' SET nota_valori='$nota' WHERE nota_id=$id";
        $executa= mysql_query($sql,$con) or die(mysql_error());

}

echo "Registro salvo com sucesso";
?>

Just sample data so you have an idea of what to do.

    
09.05.2018 / 21:30