1

I wanna edit an info of a checkbox in a page but when I clicked the edit link I want the already selected multiple checkbox from the database to be set as checked. But the result I get from my code is that only the last selected checkbox is set as checked(ticked) instead of all the selected checkbox.

Here's my code:

<td><input type = "checkbox" name = "checkbox[]" value = "Badminton" <?php if($interest == 'Badminton') echo "checked = 'checked'"; ?>/>Badminton</td>
<td><input type = "checkbox" name = "checkbox[]" value = "Baseball" <?php if($interest == 'Baseball') echo "checked = 'checked'"; ?>/>Baseball</td>
<td><input type = "checkbox" name = "checkbox[]" value = "Basketball" <?php if($interest == 'Basketball') echo "checked = 'checked'"; ?>/>Basketball</td>
<td><input type = "checkbox" name = "checkbox[]" value = "Cricket" <?php if($interest == 'Cricket') echo "checked = 'checked'"; ?>/>Cricket</td>
<td><input type = "checkbox" name = "checkbox[]" value = "Football" <?php if($interest == 'Football') echo "checked = 'checked'"; ?>/>Football</td>

and heres my sql code:

$sql = "SELECT * FROM interest where username = '$username'";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
    while($row = $result->fetch_assoc())
    {
        $interest = $row['interest_name'];
    }
}
5
  • 1
    you need to make the creation of the <tds happen within the while loop, so that you can use the current value of $interest at each iteration of the loop. Commented Feb 27, 2019 at 12:14
  • 1
    What wrong you are doing is that every time you are overriding the $interest. You need to implement some logic to save all values in an array and then in your PHP code put check accordingly whether the value exists or not. Edit: See the answer of @devpro Commented Feb 27, 2019 at 12:20
  • @ADyson I don't think that makes any difference(I have checked after your help). Commented Feb 27, 2019 at 12:21
  • Clearly, I'm the exception, not the rule, but I just don't answer questions that begin 'I wanna' Commented Feb 27, 2019 at 12:22
  • @JayeshDhandha: thank you for recommendation :) Commented Feb 27, 2019 at 12:23

2 Answers 2

4

First of all your if you want to use all the values coming from your query you need to store them in an array something like:

while($row = $result->fetch_assoc())
{
    $interest[] = $row['interest_name'];
}

Now when it comes to your html you can go like:

<td><input type = "checkbox" name = "checkbox[]" value = "Football" <?php if(in_array('Football',$interest)) echo "checked = 'checked'"; ?>/>Football</td>

Just the first example, you should do it in all of them.

Sign up to request clarification or add additional context in comments.

1 Comment

can you help me for the next step @pr1nc3 on how to update that updated checkbox in database
3

You can store your values in to an array then you can use in_array() to check:

<?php
$interest = array(); // initializing  
while($row = $result->fetch_assoc())
{
    $interest[] = $row['interest_name']; // store in an array
}
?>

HTML:

<td>
     <input type = "checkbox" name = "checkbox[]" value = "Badminton" <?=(in_array('Badminton',$interest) ? 'checked="checked"' : '')?> />Badminton
</td>

Side Note: Change other checkbox as same above with different values which you have.

9 Comments

@SangTonsing: print_r($interest) check what r u getting in this and share
I have tried but still none is selected. Anyway @pr1nc3 gave the right one. But really appreciate for your [email protected] You
both code same actually i was using 'checked=""' which was not working in your code so you need to change 'checked=""' with 'checked="checked"' if want to use ternary operator @SangTonsing
Yeah good to know that too, Thank you so much for your help @devpro
can you help me for the next step on how to update the value of that updated checkbox in a database @devpro
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.