The following code allows you to choose (1) size and (1) or more colors associated with that size. Only problem... I can't figure out how to get it to work. (Currently the code will work and insert just one entry upon submit; the last color to be loaded)
I suppose the color should be posted as an array, then when retrived through the forms' post, operate in a while loop to insert multiple entries in the dB. Any help will be greatly appreciated.
(itemtrial1.php)
<html>
<body>
<?PHP
// Conects to the SERVER and the DATABASE
include("dbconnect.inc");
// Creates the form
echo "<form action=\"additemtrial2.php\" method=\"post\">";
echo "<table>";
echo "<tr>";
echo "<th>Choose Size</th>";
echo "<th>Choose Colors</th>";
echo "</tr>";
echo "<tr>";
echo "<td><select name=\"size_id\">";
$sizes = @mysql_query("SELECT * FROM sizes");
while ($dropdownrow = mysql_fetch_array($sizes) ) {
$id = $dropdownrow["size_id"];
$size_word = $dropdownrow["size_word"];
echo "<option value=\"$id\">$size_word";
}
echo "</select></td>";
$colors = @mysql_query("SELECT * FROM colors ORDER BY color_name ASC");
while ($checkboxoptions = mysql_fetch_array($colors) ) {
$id2 = $checkboxoptions["color_id"];
$color_name = $checkboxoptions["color_name"];
echo "<td><input type=\"checkbox\" name=\"color_id\" value=\"$id2\">$color_name</td>";
}
echo "</tr>";
echo "<tr><td><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>";
echo "</table>";
echo "</form>";
?>
</body>
</html>
(itemtrial2.php)
<html>
<head>
</head>
<body>
<?PHP
// Conects to the SERVER and the DATABASE
include("dbconnect.inc");
// Inserts the variables into table ITEMTRIAL
$sql = "INSERT INTO itemtrial
SET size_id='".$_POST['size_id']."',
color_id='".$_POST['color_id']."'";
if (mysql_query($sql)) {
echo( "<p>The item trial has been added</p>");
}
else {
echo("<p>Error adding the item trial.</p>");
}
?>
</body>
</html>