为什么我打印3次输出?
问题描述:
当学生ID“sid”在搜索框中输入时,我需要从“stud”表中返回一些学生的详细信息。同样在第二部分中,我需要在我的数据库“enrl”中搜索注册表以获取此给定学生ID的注册课程的UCAS ID。为什么我打印3次输出?
问题是为什么我会搜索我的搜索的第二部分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>
答
问题:
现在的问题是,为什么让我的搜索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
,然后搜索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中被删除。改为使用mysqli
或PDO
扩展名。这是why you shouldn't use mysql_ functions
+0
非常欢迎! :) –
你有三行数据相同吗?你的代码是按照SQL注入开放的。你应该更新到'PDO'或'mysqli'驱动程序。 – chris85