猿问

mysql 中的第四列数据没有保存 Cookies?

我只是在构建一个简单的待办事项列表。我构建了复选框,将其保存为 cookie,以便下次用户可以看到选中的复选框。

我的代码的一部分:


//this code is in a table which shows checkboxes in each row.

//tasks is the query which selects all from the database

<form action="index.php" method="POST" id="checksubmit" name="checksubmit">

    <?php

    $i = 1;

    while ($row = mysqli_fetch_array($tasks)) { ?>

        <tr>

            <td class="tick"><div class="custom-control custom-checkbox">

                <input  type="checkbox" class="custom-control-input" id="check<?php echo $row['id'];?>" name="arraycheck[]" value="<?php echo $row['id'];?>"

                <?php

                if ((((isset($_POST['arraycheck'])) && (in_array($row['id'], $_POST['arraycheck'])))) || (isset($_COOKIE[$row['id']]))) {

                    if (!isset($_COOKIE[$row['id']])) {

                        setcookie ($row['id'],"checked='checked'",time()+3600000);

                    }

                }

                if ((isset($_POST['button-submit'])) && (!in_array($row['id'], $_POST['arraycheck']))) {

                    setcookie ($row['id'],"",time()-3600);

                }


                 echo $_COOKIE[$row['id']];

                ?>>

                <label class="custom-control-label" for="check<?php echo $row['id'];?>"></label>

                </div>

            </td>

        </tr>

    <?php

        $i++;

    }

    ?>

代码工作得很好!复选框由 cookie 保存。

但问题:只有前四行有效,从第五行开始,一切都不起作用。多么奇怪的错误!


一只萌萌小番薯
浏览 154回答 2
2回答

开满天机

您的代码存在一些问题:该setcookie()函数调用不会在所有的工作和将失败,“头已经发送”警告。这是您的代码不起作用的主要原因。您在没有table 的情况下使用tr。如果您有表格之外的表格,我认为这是无效的。至少在您发布的代码中,表单没有关闭。如果未设置 cookie,此行会产生“未定义偏移”警告:回声 $_COOKIE[$row['id']];你还应该缩进你的代码,因为那样你可以更好地看到错误。如果尚未完成,请将其添加到开头以查看错误和警告:error_reporting(E_ALL&nbsp;|&nbsp;E_NOTICE);

狐的传说

完整代码:&nbsp;<table class="table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead class="thead-dark">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>STT</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Lời nhắc</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 50px;">Xong?</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th style="width: 50px;">Ảnh</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="index.php" method="POST" id="checksubmit" name="checksubmit">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> <?php echo $i; ?> </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="task"> <?php if(isset($_COOKIE[$row['id']])) {echo '<span style="color:gray;"><strike>';} echo $row['task']; if(isset($_COOKIE[$row['id']])) {echo '</strike></span>';} ?> </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="tick"><div class="custom-control custom-checkbox">&nbsp; &nbsp; <input&nbsp; type="checkbox" class="custom-control-input" id="check<?php echo $row['id'];?>" name="arraycheck[]" value="<?php echo $row['id'];?>"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((((isset($_POST['arraycheck'])) && (in_array($row['id'], $_POST['arraycheck'])))) || (isset($_COOKIE[$row['id']]))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isset($_COOKIE[$row['id']])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setcookie ($row['id'],"checked='checked'",time()+3600000);&nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; ((isset($_POST['button-submit'])) && (!in_array($row['id'], $_POST['arraycheck']))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setcookie ($row['id'],"",time()-3600);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $_COOKIE[$row['id']];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ?>>&nbsp; &nbsp; <label class="custom-control-label" for="check<?php echo $row['id'];?>"></label>&nbsp; </div></td>&nbsp; <td><?php if (!empty($row['image'])) { ?> <a href="view.php?idimg=<?php echo $row['id']?>" class="btn btn-primary btn-sm"><i class="fas fa-image"></i></a> <?php } ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php $i++; } ?>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="submit" value="Lưu" class="btn btn-primary" name="button-submit">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</table>
随时随地看视频慕课网APP
我要回答