为什么我的输出打印3次?

时间:2015-11-26 00:32:30

标签: php mysql sql

当我在搜索框中输入学生ID“sid”时,我需要从“stud”表中返回一些学生详细信息。同样在第二部分中,我需要在我的数据库“enrl”中搜索注册表,以获得该给定学生ID的注册课程的UCAS ID。

问题是为什么我会在搜索的第二部分进行3次搜索? 这是我的代码:

<?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

1 个答案:

答案 0 :(得分:0)

<强>问题:

  

问题是为什么我会在搜索的第二部分进行3次搜索?

<强>解决方案:

那是因为您没有搜索特定学生的注册记录。

在所有区块之外取$id,如下所示:

// 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

然后针对特定的学生ID搜索enrl表,如下所示:

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

旁注:请不要使用mysql_数据库扩展,它们在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。请改用mysqliPDO扩展程序。这是why you shouldn't use mysql_ functions