Jump to content
joaocasta

Como fazer updates com checkbox

Recommended Posts

joaocasta

Boas tardes pessoal do PAP.

Estou a tentar utilizar umas checkboxes para que certas mensagens sejam actualizadas na base de dados.

Têm de ser aprovadas. mas não estou a conseguir usar o update.

Podem ajudar-me? obrigado.

index.php


<?php

$username = "root";
$password = "";

   $conn = new PDO('mysql:host=localhost;dbname=chat_addon', $username, $password);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$data = $conn->query('SELECT * FROM chat_messages WHERE approved = 0');
?>
<form action="approvemessages.php" method="POST">
<?php
foreach($data as $row) {
echo $row['message'];
?>
<input type="checkbox" name="message[]" value="<?php echo $row["id"]; ?>" /><br/>
<?php
}
?>
<input type="submit" name="submit" value="Submit">
</form>

approvemessages.php


<?php

$username = "root";
$password = "";

   $conn = new PDO('mysql:host=localhost;dbname=chat_addon', $username, $password);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// checkboxes are only sent if checked
if (!empty($_POST["message"]) && is_array($_POST["message"])) {
   // sanitize
   $ids = array_map("intval", array_filter($_POST["message"], "is_scalar"));
   // update all of them at once in batches of 1000
   foreach (array_chunk($ids, 1000) as $batch) {
       mysql_query("UPDATE chat_messages SET approve = 1 WHERE id IN (" . implode(", ", $batch) . ")");
   }
}
?>

Share this post


Link to post
Share on other sites
brunoais

Toma atenção a como tentas executar o query SQL. Se tomasses atenção ao erro que te aparece ajudava muito...


"[Os jovens da actual geração]não lêem porque não envolve um telecomando que dê para mirar e atirar, não falam porque a trapalhice é rainha e o calão é rei" autor: thoga31

Life is a genetically transmitted disease, induced by sex, with death rate of 100%.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.