我正在研究验证系统,我的数据库中有几个msg值为0(这意味着它们尚未被接受)所以我创建了一个页面,如果选中验证复选框,则将值更改为1。
这里是代码:
<?php
if (isset($_POST['valider']))
{
$reponse1 = db_query('UPDATE messages SET value = 1 WHERE id = ?', array($donneesmsg['id']));
echo "NSF validée";
}
if (isset($_POST['delete']))
{
$reponse1 = db_query('DELETE FROM messages WHERE id = ?', array($donneesmsg['id']));
echo "NSF supprimée";
}
$reponse = db_query('SELECT * FROM messages WHERE value = 0');
while ($donneesmsg = $reponse->fetch()) {
?>
<h5><?php echo $donneesmsg['Pseudo']; ?></h5>
<p class="text-center text-align"><?php echo $donneesmsg['Message']; ?></p>
<form class="form-inline" method="post" action="index.php?page=changeto1">
<label class="checkbox">
<input type="checkbox" name="valider" value="1">
Valider
</label>
<label class="checkbox">
<input type="checkbox" name="delete">
Supprimer
</label>
<input type="text" name="id" value="<?php echo $donneesmsg['id']; ?>">
<?php
}
?>
<br>
<br>
<button type="submit" class="btn">Envoyer</button>
</form>
但我有一个问题,当提交页面时,变量$ donneesmsg ['id']不再存在,因为变量首先在while循环中定义。
不幸的是,我真的需要这个ID来让我的系统正常工作。 我怎样才能获得这个ID?
答案 0 :(得分:0)
你把循环放在了错误的地方。通过循环方式,它将为每个复选框提供一个开始表单标记。获得多个不同复选框的好方法是将其设置为数组。为了做到这些,它看起来像这样:
<form class="form-inline" method="post" action="index.php?page=changeto1">
<?php
$reponse = db_query('SELECT * FROM messages WHERE value = 0');
while ($donneesmsg = $reponse->fetch()) :
?>
<h5><?php echo $donneesmsg['Pseudo']; ?></h5>
<p class="text-center text-align"><?php echo $donneesmsg['Message']; ?></p>
<label class="checkbox">
<input type="checkbox" name="valider[<?php echo $donneesmsg['id']; ?>]" value="1">
Valider
</label>
<label class="checkbox">
<input type="checkbox" name="delete[<?php echo $donneesmsg['id']; ?>]">
Supprimer
</label>
<?php endwhile; ?>
<br>
<br>
<button type="submit" class="btn">Envoyer</button>
</form>
然后在提交表单的页面上,您可以执行以下操作:
<?php
$idsToValidate = array_keys($_POST['valider']);
$idsToDelete = array_keys($_POST['delete']);
此外,如果您想要而不是<input name="delete[*id*]" value="1" />
,您可以执行<input name="delete[]" value="*id*" />
然后您不需要对结果执行array_keys它已经是您要使用的ID数组
答案 1 :(得分:0)
我应该如何设置它?
<?php
$idsToValidate = array_keys($_POST['valider']);
$idsToDelete = array_keys($_POST['delete']);
if (isset($idsToValidate))
{
$reponse1 = db_query('UPDATE messages SET value = 1 WHERE id = ?', array($idsToValidate['id']));
echo "<center>NSF validée</center>";
}
if (isset($idsToDelete))
{
$reponse1 = db_query('DELETE FROM messages WHERE id = ?', array($idsToDelete['id']));
echo "<center>NSF supprimée</center>";
}
?>
<form class="form-inline" method="post" action="index.php?page=changeto1">
<?php
$reponse = db_query('SELECT * FROM messages WHERE value = 0');
while ($donneesmsg = $reponse->fetch()) :
?>
<h5><?php echo $donneesmsg['Pseudo']; ?></h5>
<p class="text-center text-align"><?php echo $donneesmsg['Message']; ?></p>
<label class="checkbox">
<input type="checkbox" name="valider[<?php echo $donneesmsg['id']; ?>]" value="1">
Valider
</label>
<label class="checkbox">
<input type="checkbox" name="delete[<?php echo $donneesmsg['id']; ?>]">
Supprimer
</label>
<?php endwhile; ?>
<br>
<br>
<button type="submit" class="btn">Envoyer</button>
</form>