数据库检索的值是从复选框中选择的,并在另一个页面中查看,但当重新加载页面时,结果不会出现如何解决?

xjreopfe  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(265)

使用php,我获取了一个表中的所有记录,用户可以使用复选框从中选择特定的记录。所选记录将在其他页面中查看。但问题是重新加载页面时,不会查看结果。
此代码用于从数据库获取所有记录并使用复选框提交

<div class="col-lg-8 card">
      <table class="table table-striped table-hover table-bordered datatable">
    <thead style="background: #967272; color: black;">
    <tr>
        <th style="width: 50px;"></th>
        <th style="color: #3b3b3b;">Company Name</th>
        <th style="color: #3b3b3b;">Service</th>
        <th style="color: #3b3b3b;">email</th>
        <th style="color: #3b3b3b;">Logo</th>
    </tr>
    </thead>
    <tbody>
        <form name="" action="index.php" method="POST">
    <?php
        $sql="select * from company_details ";
        $result = $con->query($sql);
        if ($result->num_rows > 0) {
        ?>

        <?php
        while($row = $result->fetch_assoc()) {
        ?>

        <tr>
            <td>
                <center><input type="checkbox" name="check[<?php echo $row['id'];?>]" value="1"></center>

            </td>
            <td><input type="" name="company_name[]" value="<?php echo $row['company_name'];?>"></label></td>
            <td><input type="" name="service[]" value="<?php echo $row['service'];?>"></label></td>
            <td><input type="label" name="email[]" value="<?php echo $row['email'];?>"></label></td>
            <td><?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?></td>

        </tr>
    <?php }} //end of while loop ?>

  </div>
  <button type="submit" class="btn btn-hg btn-success" name="AddEbooks">Submit</button>
</form>

此代码显示选定的记录

<?php include 'config.php';?>
<?php

    session_start();
    if(!isset($_POST['check']))

     {
?>
<?php
        $sql="select * from company_details";
        $result = $con->query($sql);
        if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()) {

        $ID=$row['id'];
        /* Get the Book ID and now we have to fetch from $_POST the value from the form */
        if (array_key_exists($ID, $_POST["check"])) {
            $ischecked=$_POST["check"][$ID];
            /* See if this has a value of 1.  If it does, it means it has been checked */
            if ($ischecked==1) {
        ?>

        <div class="col-lg-6" style="padding-top: 20px;">
        <section>
            <div class="well" >
                <div class="card" >
                    <div class="row ">
                        <div class="col-md-4">
                            <?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
                        </div>
                        <div class="col-md-8 px-3">
                            <div class="card-block px-3">
                            <h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
                            <div >
                                <label>Service Type</label>
                                <?php echo $row ['service']; ?>
                            </div>
                            <div >
                                <label>email</label>
                                <?php echo $row ['email']; ?>
                            </div>
                            <div >
                                <label>About</label>
                                 <?php echo substr($row['details'], 0, 300); ?>
                            </div>

                                <a href="#" class="btn btn-success">Read More</a>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

        <?php

            }
        }
    }
}
?>
6gpjuf90

6gpjuf901#

存储 $_POST["check"] 在一个会话变量中,并使用该会话变量检查 array_key_exists 更新

<?php 
    session_start();

    include 'config.php';

    if(!isset($_SESSION['checked'])){
        $_SESSION['checked']=$_POST['check'];
    }

    $checked = $_SESSION['checked'];

    if(!empty($checked))
    {

        $sql="select * from company_details";
        $result = $con->query($sql);
        if ($result->num_rows > 0) 
        {

            while($row = $result->fetch_assoc()) 
            {

            $ID=$row['id'];
            /* Get the Book ID and now we have to fetch from $_POST the value from the form */
            if (array_key_exists($ID, $checked)) {
                $ischecked=$checked[$ID];
                /* See if this has a value of 1.  If it does, it means it has been checked */
                if ($ischecked==1) 
                {
                    ?>

                    <div class="col-lg-6" style="padding-top: 20px;">
                    <section>
                        <div class="well">
                            <div class="card" >
                                <div class="row ">
                                    <div class="col-md-4">
                                        <?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
                                    </div>
                                    <div class="col-md-8 px-3">
                                        <div class="card-block px-3">
                                        <h4 class="card-title"><?php echo $row ['company_name']; ?></h4>
                                        <div >
                                            <label>Service Type</label>
                                            <?php echo $row ['service']; ?>
                                        </div>
                                        <div >
                                            <label>email</label>
                                            <?php echo $row ['email']; ?>
                                        </div>
                                        <div >
                                            <label>About</label>
                                             <?php echo substr($row['details'], 0, 300); ?>
                                        </div>

                                            <a href="#" class="btn btn-success">Read More</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>

            <?php

                }
            }
        }
    }

   }
?>

相关问题