Click here to Skip to main content
15,903,743 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am trying to compare the two table values and need to make checked the matched checkbox values, I am using the below code and it is not working

<div class="multiselect">
       	 <?php
		   $sql1="SELECT id, first_name, last_name, chat_id from users_master";
           $result1 = mysqli_query($db,$sql1);
  
		   $sql20="select * from task_assigned_to where id='$getid'";
			$result20=mysqli_query($db, $sql20);
			
			
		while ($row1 = mysqli_fetch_array($result1))  {
			
			
			?>
    <label><input type="checkbox" name="taskers[]" value="<?php echo $row1['chat_id']; ?>" <?php 
				  
				  while($row10=mysqli_fetch_array($result20))
				  {
					  
					  if($row10['assigned_to_id']==$row1['chat_id'])
					  {
						  ?>checked<?php
					  } 
				  }
				  
				  ?> /> <?php echo $row1['first_name']. " " .$row1['last_name']; ?> </label>
    <?php		
		}?>
		   </div>


What I have tried:

I tried changing all the possible ways from the articles
Posted
Updated 13-Nov-20 3:32am
Comments
Richard Deeming 13-Nov-20 4:29am    
$sql20="select * from task_assigned_to where id='$getid'";

Your code is potentially vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

1 solution

You need to redesign your SQL -

Look into the idea of JOIN's.

Depending upon what you want to see, you can get the list with the answer pre-done for you.

If you only want a list of matches, use an INNER JOIN.
If you want a list of matches and un-matched, use something like a LEFT JOIN and perhaps add an ISNULL() check if it'll make things simpler for you.

Basically, I'm saying, let SQL do the work.
 
Share this answer
 
v2

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