-1

我正在开发一个验证系统,我的数据库中有几个值 = 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?

4

2 回答 2

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 的数组

于 2013-07-02T05:11:44.543 回答
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>
于 2013-07-02T08:08:12.173 回答