Okay, first off, I am pretty much a newbie and coding. I am self taught so obviously there are things that I haven't learned about yet. I am writing a simple html/php application that interacts with a MySQL database. Here is the function of it.
Visit a webpage and be presented with records from that db table, edit, delete, and add records from that html form, as well as filter out your results to slim the list down. This is a license tracking application for software licenses. I have done all of the above except applied the filtering function in its entirety. I have added a two drop down menus that populate based on values currently in the database table. I have one of them working, which is a department filter. It will successfully pass the value from the drop down menu to the next page and as a result only show you records that contain that value.
Here is my code for my filtering drop down options the file name is view.php
<?php
$sql2 = "SELECT DISTINCT dept FROM sw_lic WHERE dept != ''";
$sql3 = "SELECT DISTINCT sw_name FROM sw_lic WHERE sw_name != ''";
$result2 = mysql_query($sql2);
$result3 = mysql_query($sql3);
?>
<form action="filter_view.php" method="POST">
<select name="dept">
<?php
while($nt=mysql_fetch_array($result2))
{
echo "<option name='dept' value=$nt[dept]>$nt[dept]</option>";
}
?>
</select>
<select name="sw_name">
<?php
while($st=mysql_fetch_array($result3))
{
echo "<option name='sw_name' value=$st[sw_name]>$st[sw_name]</option>";
}
?>
<input type="submit" name="submit" value="Submit">
And here is the script that the variables should be passed to, the file name is filtered_view.php
<?php
// connect to the database
include('connect-db.php');
// get results from database
$nt = $_POST["dept"];
$st = $_POST["sw_name"];
$result = mysql_query("SELECT * FROM sw_lic WHERE dept='$nt' AND sw_name='$st'")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10' background-color='#000066' color='#FFFFFF'>";
echo "<tr> <th>Purchased for</th> <th>Installed On</th> <th>Dept.</th> <th>Software Name</th> <th>Software Ver.</th> <th>Software Origin</th> <th>LIC Key</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['sysname'] . '</td>';
echo '<td>' . $row['dept'] . '</td>';
echo '<td>' . $row['sw_name'] . '</td>';
echo '<td>' . $row['sw_ver'] . '</td>';
echo '<td>' . $row['origin'] . '</td>';
echo '<td>' . $row['sw_key'] . '</td>';
echo '<td><a href="edit.php?sw_id=' . $row['sw_id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?sw_id=' . $row['sw_id'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
If I comment out the second drop down menu 'sw_name' and only implement the 'dept' drop down menu, the variable is successfully passed to the filtered_view.php and I only see records that have a dept field value that matches my drop down selection value. As soon as I implement the second drop down menu I get no results. If I statically place a value in the SELECT command on filter_view.php then everything works. I think that the $st variable just isn't being injected into the SELECT string.
Can anyone help me? As I said, I am self taught, so I am sure my code is horribly messy, and I will welcome any tips or recommendations. I have posted on other forums and recieved no attention in this issue. A friend of mine recommended this site as he has had great success working through problems with the help of this community.
I hope someone can help a self taught idiot like me figure this out. 🙂
Thanks so much in advance.