Connect table with other table

Hi all,

I am having the following problem. I have made a page where I can add new items, which can be connected with multiple categories. Please see below the part of the page / form, which will lead to new_add.php:

[code]new.php

Website
E-mail
				<?php 
				$query=mysqli_query($conn, "select * from category")or die(mysqli_error($conn));
				while($row=mysqli_fetch_array($query)){
					$category_id=$row['id'];
				?>
<?php echo $row['type'] ?> <?php echo $row['name'] ?>
<?php } ?>[/code]

[code]new_add.php

<?php $url = check($_POST['url']); $email = check($_POST['email']); $id=$_POST['selector']; $N = count($id); for($i=0; $i < $N; $i++) { $result = mysqli_query($conn, "SELECT * FROM category where id='$id[$i]'"); while($row = mysqli_fetch_array($result)) { } } $sql = "INSERT INTO test(id, url, email, category_id) VALUES (NULL, '{$url}', '{$email}', '{$category_id}');"; $retval = mysqli_query($conn, $sql); if(! $retval ) { die('Could not enter data: ' . mysqli_error($conn)); } ?>[/code]

I want to connect the with checkboxes selected categories to the other table (test), however I cannot get it to work. I must be missing something. Hope somebody can point me in the right direction and help me out. It would be much appreciated.

Many thanks in advance.

Anybody, please? I would be gratefull!

Its probably because ur form is messed up. You start the form outside the loop, but complete inside the loop, so what you end up with is

Given the form structure, I’m not really sure how you would want to fix it, but you either need to move the inside the while loop or move the outside of the loop.

The simplest way to see if I’m right, is to look at the html output (right click on the screen and click on view source).

Thanks, but that’s not it :).

I’m now able to get all selected checkbox ID’s into table test, column category_id. I’m aware this is not the solution I need. What I need is a many to many relationship.

To begin I have made a setup, however I don’t get it to work. It’s quite complicated. With this setup I want to edit an item. I’m positive if I know how to do this, I know how to add new items to table test and add the categories to the table that will be setup for the many to many relationship. For this many to many relationship I have made the following link table :

link_category_test kolom "category_id" kolom "test_id"

What I want is to fetch an overview of all categories in table category, and only the categories that have been added to a test-item from table test may a checkbox be selected. Offcourse which ones these are is fetched from the link_category_test table. If a category is not connected with a test-item, the category will remain unchecked :

[code]$id=$_POST['selector']; $N = count($id); for($i=0; $i < $N; $i++) { $result = mysqli_query($conn, "SELECT * FROM test where id='$id[$i]'"); while($row = mysqli_fetch_array($result)) { ?>
Categorie
		<table cellpadding="0" cellspacing="0" border="0">
				<?php 
				$query3=mysqli_query($conn, "SELECT category.id, category.name, category.type, test.id, test.name,  

CASE WHEN link_category_test.test_id IS NULL THEN ‘F’ ELSE ‘T’ END AS selected
FROM category
CROSS JOIN test
LEFT JOIN link_category_testON (link_category_test.category_id = category.id AND link_category_test.test_id = test.id)
WHERE test.id=’$id[$i]’");

				while($row=mysqli_fetch_array($query3)){
					$category_id=$row['id'];
				?>
				<tr>
					<td width="20" style="padding-bottom: 4px"><input name="selector[]" type="checkbox" value="<?php echo $category_id; ?> <?php if ($row[test.id'] ==1) { echo "checked";} else {} ?>"></td>
					<td width="100" style="padding-top:3px; padding-bottom: 1px"><?php echo $row['type'] ?></td>
    	        	<td width="265" style="padding-top:3px; padding-bottom: 1px"><?php echo $row['name'] ?></td>
				</tr>
				<?php  } ?>
		</table>
        
</div>
</div>  
</div>
<?php } } ?>[/code]

As you can see I’m in the right direction, however I cannot seem to get it to work.
I was hoping somebody could help me a bit with some guidance.
All help is so much appreciated!

Sponsor our Newsletter | Privacy Policy | Terms of Service