Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have developed a code to fetch all records from database using PHP . and the user can select some records as he wish using checkbox. The selected records are shown in a separate page.but when page is reloaded the results are not shown.how can I solve this?

What I have tried:

This code is used for fetching all records from database and use checkboxes to submit
<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>


This code shows the selected records
<div class="col-lg-8">

        <?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

            }
        }
    }
}
?>

</div> 
Posted
Comments
Richard Deeming 7-Aug-18 13:17pm    
What do you mean by "when the page is reloaded"? Are you submitting the form again? Or just making a new GET request to the page?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900