I have a form which connects to a Mysql database. Unfortunately it doesn't seem to be working properly.
I have this application working where I can put in all my projects that I am working on and all the products I have used on each project. On the input form for the Products, it connects to the Mysql database to pull up a list of all my projects that I am working on and puts checkboxes next to them so I can select which projects go with that product.
That part works perfectly (just gave you the background so you can see what I am trying to do)
Now where I am having problems is an update form. I am trying to make the same form except to edit and update those products. Right now all it does is counts the number of projects it is linked to and lists those projects.
I am trying to make it look like this
Associated Projects:
o Project1
o Project2
X Project3
X Project4
o Project5
X Project6
Instead I am getting
Associated Projects:
X Project6
X Project6
X Project6
Here's the relevent code
<?php
// Sets the Database Settings
$table="projects";
// Connects to the MYSQL Database and Selects the right Table
include("includes/dbconfig.inc.php");
$query="SELECT * FROM $table ORDER BY name ASC";
// Counts the number of entries in the Database
$result=mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);
mysql_close();
$i=0;
$j = 0;
include("includes/dbconfig.inc.php");
//get all projects by product id
$qry = "SELECT b.Id as product_name ,c.Id as project_name
FROM joins AS a
LEFT JOIN products as b on a.product_id = b.Id
LEFT JOIN projects as c on a.project_id = c.Id
WHERE a.product_id = $product";
// Counts the number of entries in the Database
$result2=mysql_query($qry) or die(mysql_error());
mysql_close();
$num2=mysql_numrows($result2);
// Loops writing this the same number of times as entries
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$id222=mysql_result($result,$i,"Id");
while ($j < $num2) {
$project=mysql_result($result2,$j,"project_name");
if($project = $id222) {
echo "<input type=checkbox name=proj[] value=$id222 checked>$name<br>";
} else {
echo "<input type=checkbox name=proj[] value=$id222 >$name<br>";
}
$j++;
}
$i++;
}
?>
Hopefully you can make sense of that, if you need more info let me know