0

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>

RESULT

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
user3050565
  • 37
  • 2
  • 2
  • 7
  • Do you have three rows with the same data? Your code is open to SQL injections as is. You should update to `PDO` or `mysqli` driver. – chris85 Nov 26 '15 at 00:38

1 Answers1

0

Problem:

The question is why do I get the second part of my search 3 times?

Solution:

That's because you're not searching for enrollment record of a particular student.

Take the $id outside of all blocks, like this:

// your code

$id = '';
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';
        }

        // your code
    }

}

// your code

And then search the enrl table against the particular student id, like this:

//Search "enrl" Table
$query = mysql_query("SELECT * FROM enrl WHERE sid = '$id'") or die("Could not Search");
// your code

Sidenote: Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37