Save more than one PHP and MYSQL record

Asked

Viewed 243 times

1

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

Print da tela com as informações

This information is from a bank, so are different ids each student.
My problem is when I click the save button, because when I click it only saves the last item. What can I do in this case?

Code used

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

In the 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";

1 answer

2


You must turn your form fields into one array and store the id in a field hidden. for example:

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

And in the php form when you receive the data, just go through with 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";
?>

These are just examples for you to get an idea of what to do.

  • I tried to use this but the following error&#Xa is appearing;Fatal error: Call to a Member Function prepare() on Resource in C: xampp htdocs school admin update.php on line 162

  • 1

    You are using php mysql and not Pdo, I will change the answer

  • Thank you very much, it worked.

  • 1

    @Danielalencarsouza, you’re welcome!

Browser other questions tagged

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