I have three tables:
tableA
tableA_id
tableA_name
tableB
tableB_id
tableB_name
tableAtableB
tableA_id
tableB_id
A list of checkboxes is created from tableA:
function createcheckboxes(){
$checked = (isset($checked))?$checked:"";
$sql = "SELECT tableA_name FROM tableA";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
echo "<input type='checkbox' name='item[]' value='{$row['tableA_name']}' $checked />";
echo $row['tableA_name'] . "<br />";
}
}
<form action="" method="POST">
<?php createcheckboxes();?>
<input type="submit" name="submit" value="Submit" />
</form>
The following code displays a list of checked items:
$list = (isset($_POST['item']))?$_POST['item']:"";
if(empty($list))
{
echo("You didn't select any items.");
}
else
{
$N = count($list);
echo("You selected $N item(s): <br />");
for($i=0; $i < $N; $i++)
{
echo($list[$i] . " <br />");
}
}
This all works fine. However, I now want to display a list of tableB names dependent on the checkbox selection from tableA. TableB is linked to tableA via TableATableB. I just can't get the query correct. I know I'm way off but I just can't figure how to do it. This is what I have so far:
$list = (isset($_POST['item']))?$_POST['item']:"";
$sql = "SELECT * FROM tableB WHERE tableB_name IN (SELECT DISTINCT tableAtableB.tableB_id FROM tableAtableB WHERE tableAtableB.tableA_id =" . $list or die(mysql_error());
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
echo $row['tableB_name'];
}
I have used print_r and the array contains the correct values:
print_r(array_values($list));
How do I get my code to display a list of tableB_name?