I need to return some student details from "stud" table when Student IDs "sid" are entered in search box. Also in second section, I need to search enrolment table in my database "enrl" to get UCAS ID of enrolled course of this given Student ID.
The question is why do I get the second part of my search 3 times? here is my code:
<?php
mysql_connect("localhost","root","") or die ("Could Not Connect!");
mysql_select_db("swanseauniversity") or die ("Could Not Connect to         database!");
$output = '';
$output2 = '';
//Collect
if(isset($_POST['search'])){
    $searchq = $_POST['search'];
//Search "stud" table   
    $query = mysql_query("SELECT * FROM stud WHERE sid LIKE '$searchq'") or    die("Could not Search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output = 'there was no search result';
    }
    else {
        while($row = mysql_fetch_array($query)){
            $id = $row['sid'];
            $title = $row['title'];
            $fname = $row['firstname'];
            $lname = $row['lastname'];
            $DOB = $row['dob'];
            if ($row['gender'] == 'f'){
                $gender = 'Female';
            }
            if ($row['gender'] == 'm'){
                $gender = 'Male';
            }
            $output .= 
            '<table style="width:100%", frame="box">'
                .'<tr>'
                    .'<td>'.'Student ID'.'</td>'
                    .'<td>'. $id.'</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'Title'.'</td>'
                    .'<td>'. $title.'</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'Fullname:'.'</td>'
                    .'<td>'. $fname.' '.$lname.'</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'Date of Birth:'.'</td>'
                    .'<td>'. $DOB.'</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'Gender'.'</td>'
                    .'<td>'. $gender.'</td>'
                .'</tr>'
            .'</table>'
            ;
        }   
    }
    //Search "enrl" Table
    $query = mysql_query("SELECT * FROM enrl WHERE sid LIKE '$searchq'") or     die("Could not Search");
    $count = mysql_num_rows($query);
    if($count == 0){
        $output2 = 'there was no search result';
    }
    else {
        while($row = mysql_fetch_array($query)){
            $enrolledcourse = $row['pid'];
            $output2 .= 
            '<table style="width:100%", frame="box">'
                .'<tr>'
                    .'<td>'.'UCAS Code'.'</td>'
                    .'<td>'. $enrolledcourse.'</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'dummy1'.'</td>'
                    .'<td>'. '</td>'
                .'</tr>'
                .'<tr>'
                    .'<td>'.'dummy2'.'</td>'
                    .'<td>'. '</td>'
                .'</tr>'
            .'</table>'
            ;
        }   
    }   
}
?>
<html>
    <head>
    <title>Student Search</title>
    </head>
    <body>
<!--SEARCH FORM-->
        <form action="index.php" method="post"> 
            <label>Query by Student ID #:</label> <input type="text"     name="search" placeholder="Search for students..."/>
            <input type="submit" value="Submit"/>   
        </form>
    <hr>
    <h4 style="margin-bottom:0px">Personal Details</h4>
    <?php 
    print("$output");
    ?>
    <h4 style="margin-bottom:0px">Course Details</h4>
    <?php 
    print("$output2");
    ?>
    </body>
</html>
 
     
     
    