Ive been having problem with my codes.. Im a still noob with php so i really appreciate if anyone could help me :'( .. Im trying to display data from database based on the selected dropdown list value. There are three data currently in my database table .. However, only one show up on my output .. i dont know whats wrong with my codes. can anyone help me ..
this is my code departmentlist.php
Choose Department :
<form action="" method="POST">
<select name="department">
<option value="" selected="selected">-- Choose department --</option>
<option value="Information System Department">Information System Department</option>
<option value="Finance">Finance</option>
<option value="HR and Administration">HR and Administration</option>
<option value="Security">Security</option>
</select>
<input type="submit" value="Submit" />
</form>
</br>
<table border="1" id="table">
<tr><th bgcolor="#00CCCC">Department</th>
<th bgcolor="#00CCCC">Locaion</th>
<th bgcolor="#00CCCC">Serial Number</th>
<th bgcolor="#00CCCC">Description of asset</th>
</tr>
<?php
mysql_connect('localhost','root',"") or die('Error1 '.mysql_error());
mysql_select_db("ams") or die('error2'.mysql_error());
if($_SERVER['REQUEST_METHOD'] =='POST')
{ $dep=$_POST['department'];
$query="SELECT * FROM asset WHERE department= '" . $dep . "'";
$run=mysql_query($query);
$numrow = mysql_num_rows($run);
$row=mysql_fetch_array($run, MYSQLI_ASSOC);
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
}
?>
</table>
Manual: http://www.php.net/mysql_fetch_array
Put mysql_fetch_array in a while loop so:
while ($row = mysql_fetch_array($run, MYSQL_ASSOC)) {
echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
}
I also suspect that you should MYSQL_ASSOC not MYSQLI_ASSOC since you're using mysql not mysqli.
Take note that mysql will be deprecated so use mysqli.
you have to loop through the results.
while($row = mysql_fetch_array($run, MYSQLI_ASSOC)){
echo "<tr><td bgcolor='#00FFCC'>".$row['department']
."</td><td bgcolor='#00FFCC'>".$row['location']
."</td><td bgcolor='#00FFCC'>".$row['serialno']
."</td><td bgcolor='#00FFCC'>".$row['desc']
."</td></tr>";
}
Try using a while loop.
while($row=mysql_fetch_array($run)){
echo "<tr>
<td bgcolor='#00FFCC'>".$row['department']."</td>
<td bgcolor='#00FFCC'>".$row['location']."</td>
<td bgcolor='#00FFCC'>".$row['serialno']."</td>
<td bgcolor='#00FFCC'>".$row['desc']."</td>
</tr>";
}