-1

So I'm using a checkbox to insert data into MySql database. When check it stores "finish" When unchecked if stores "pending"

Now for an edit feature, I have to fetch and display the data from the database. I need the checkbox to be checked or unchecked if it is stored as finish or pending. Appreciate your help, thank you.

The code to fetch the data is shown below:

    <?php
        $query = ("SELECT * FROM ebook WHERE Eid='" . $Eid . "'");
        $result = mysqli_query($con, $query);
        $rows = mysqli_fetch_assoc($result);
    ?>

Displaying data and checkbox:

   <html>
   <input type="text" name="title" value="<?php echo $rows["Title"]; ?>">
   <input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" />
   </html>
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Nov 02 '22 at 20:27

1 Answers1

2

Put a condition echo inside the input.

   <input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" <?php if ($rows['status'] == 'finish') { echo "checked"; } ?>/>
Barmar
  • 741,623
  • 53
  • 500
  • 612