2

I'm checking the database and if a particular string is matched, the respective checkbox gets checked but the error is whenever a string is matched, I'm getting two checkboxes checked, one unchecked and another checked.

Here is my code:

<?php
     $result = mysqli_query($sql,"SELECT * FROM grsi "); 
     while($row = mysqli_fetch_array($result))
     {
         $focus=explode(",",$row['spr']);
?>
<input type="checkbox" value=<?php echo $row['spr']; if(in_array("Cricket",$focus)) { ?> checked="checked" <?php } ?> name="focus[]" />Cricket
<?php
     }
  exit();
?>
6
  • 1
    Show output of this while, check your database records. Commented Dec 5, 2014 at 7:57
  • my output a unchecked checkbox followed by cricket and a checked checkbox followed by cricket Commented Dec 5, 2014 at 8:04
  • 1
    what u_mulder means i guess is the example contents of $row['spr'] Commented Dec 5, 2014 at 8:05
  • i am having just one row in my database which contains Cricket in spr column Commented Dec 5, 2014 at 8:06
  • What is the output Html you are getting? Commented Dec 5, 2014 at 8:35

3 Answers 3

2
<input type="checkbox" value="<?php echo $row['spr']; ?>" <?php if(in_array("Cricket",$focus)) { ?> checked="checked" <?php } ?> name="focus[]" />Cricket

I hope this will work properly. Please let me know if it didnt work.

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

7 Comments

i tried your answer but still the same issue one unchecked and another checked checkbox
Please check the Result Array may have duplicate entry too.
GOT IT , mysqli_fetch_assoc($result)
Actually mysqli_fetch_array will return bot associative and index result array so if the result row no is 1 it will return 2. 1 for associative and another for indexed element. This is the actual issue with you.
but he's just checking $row['spr'], not for ($x in $row)..., tho i agree mysqli_fetch_assoc is probably more efficient
|
1
 <?php
 $result = mysqli_query($sql,"SELECT * FROM grsi "); 
 while($row = mysqli_fetch_array($result))
 {
 $focus=explode(",",$row['spr']);
  if(in_array("Cricket",$focus)) {
 ?>
<input type="checkbox" value=<?php echo $row['spr'];  ?> checked="checked"  name="focus[]" />Cricket

<?php
}else{
?>
  <input type="checkbox" value=<?php echo $row['spr'];  ?> name="focus[]" />Checkbox name
<?php
}
}
exit();

?>

4 Comments

still the same issue :-(
May i know string in your database
my string in spr is Cricket
So no need to exploding the string "$focus=explode(",",$row['spr']); if(in_array("Cricket",$focus))" instead of these try only "if($row['spr']=='Cricket')"
1

Your input checkbox is inside your while loop which means if there are 2 rows from the database, there will be 2 input boxes.

And based on you SQL statement, you are selecting all records(there is no WHERE statement). So if there are 10 rows in your table, there will be 10 input box. Make sure you will only select the row that you need.

1 Comment

on your suggestion, i tried adding another product in database and on executing above code i got 4 checkboxes 2 unchecked and 2 checked

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.