I have two tables. Enrollment and Product. I want to list Product on a <select>.
Within this <select>, I only want certain Product items to appear, whereby the condition is to read from Enrollment table's ProductID which is a foreign key to Product table.
How does one exclude certain results in a <select> that had already existed in a different table?
<?php
$sql = 'SELECT * FROM product ORDER BY ProductID ASC';
$result_select = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($result_select))
$rows[] = $row;
echo "<div class=\"spanstyle\">Add course/product:<select name='add_product'>";
echo "<option selected>Choose here</option>";
foreach ($rows as $row) {
echo "<option value='" . $row['ProductID']."'>" . $row['ProductName']."</option>";
}
echo "</select></div>";
$select1 = $_POST['add_product'];
if (!strpos($select1, 'Choose here')) {
$sql3="INSERT into enrollment (StudentID, ProductID) VALUES ($StudentID, $select1)";
mysql_query($sql3);
}
?>
mysql_queryinterface. It's awful and has been removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way helps explain best practices. Make sure your user parameters are properly escaped or you will end up with severe SQL injection bugs.